Saturday 24 December 2016

Include LaTeX packages in custom locations without changing environment variables

I recently had the need to include some packages with my repository, but it's not straightforward how to do this.

One way is to just dump all the necessary files at the root level of your document. This works (at least it does with pdfLaTeX), but if you have a lot of packages to include or your packages have a lot of files then this quickly becomes really messy.

Another way is to organize it nicely in a folder and then just use a relative path in usepacakge{abc}. This was nice, except for two things. If, for whatever, reason the folder containing the packages is removed but you do have the packages installed, then you'll still get a whole lot of errors as it expects them to be at that location exactly. The second and more onerous is that you're not supposed to provide a relative path here, so a lot of packages won't work (for me it was hyperref and scrextend). If it does work, you'll still get a warning about it (see the comments here for more information).

Of course, there's the recommended way of altering the environment variable TEXINPUTS. For me this wasn't ideal as I didn't want to have to run a CMake or custom setup script before compiling for a bunch of reasons. I'm sure this method works though. You can find instructions for this method here

Turns out that altering input@path{{abd/}{xyz/}} affects where usepackage searches (as usepackage is really just a wrapper around input). All you need to do is add your custom package path to input and you're good to go! Instructions for that are here

Edit:
It gets even better. Adding to the input paths also affects where documentclass searches! And (this may be good or bad) it will default to using any installed packages on the local machine before using the ones included in your packages folder. 

Thursday 4 August 2016

Some LaTeX questions and answers

Where's my table of contents?
You have to compile it twice (I use pdfLaTeX). First run generates a .toc file and the second time uses that to create a table of contents in your document.

Using \tableofcontents is giving me an error!
You need to delete your .toc file every time you switch document types.

Where's my bibliography?
I'm assuming you've already created a .bib file and have referenced it in your document using \bibliography{...} . Compile to generate an .aux file which will contain references to the citations in your document. Then compile with BibTeX which will use your document's .aux file and the specified .bib file to generate a .bbl file for your document. It will also generate a .blg file which will contain the output of running BibTex. Now compile once again with pdfLaTeX to have the .bbl file included where \bibliography{...} is.

What is a .bbl file?
Simply put, it's the same as using the embedded bibliography system but automated. BibTex will style your \bibitem{...} contents the according what you specify in \bibliographystyle{...}.

How can I modularize my document? How can I separately compile each module?
If you just want to combine multiple files you can use either \include or \input. Use \subfile if you want the ability to compile each document separately. For more details, including the pros/cons of each, see here https://en.wikibooks.org/wiki/LaTeX/Modular_Documents
 

Thursday 23 June 2016

Undefined reference

Most likely culprit is that you've either not defined the function or you included the library with the definition in the wrong order. [source]

In my case it was a bit more insidious - I was the victim of name mangling. Long story short, be extra careful when mixing C and C++. [solution]

The sad thing is I more or less encountered this problem 6 months ago, but I think I have a better understanding of it now.

Monday 13 June 2016

Miscellaneous VirtualBox Problems

My host is Windows 7 and my guest is Ubuntu Server 16.04

Screen goes black
Initially I thought this was because it was going to sleep or something. I'm still not quite sure what the problem is, but in my case I was able to get the screen back by pressing ALT.
[Source]

Symlinks in a shared folder
I'm running Windows 7, so symlinks are disabled by default due to security concerns. They're also disabled by default in shared folders by VirtualBox - also for security concerns. If you try to create a symlink in a shared folder then you'll get an error like such:
ln: failed to create symbolic link `b': Read-only file system
I did a bunch of stuff and wasted a few hours on this so at this point I'm not sure exactly what I did to get it working. First thing I did was turn on symlinks on the host side, but this may be an optional step.
  1.  Disable UAC [Source], but this may only be needed for Windows 8+
  2. Add yourself to the SeCreateSymbolicLinkPrivilege security policy (secpol.msc -> Local Policies -> User Rights Assignment -> Create symbolic links) [Source], but this may only be needed for non-admin users
  3. Restart/Log off or run gpupdate /force  
Now on the guest side you need to run 
VBoxManage setextradata YOURVMNAME VBoxInternal2/SharedFoldersEnableSymlinksCreate/YOURSHAREFOLDERNAME 1 
For the shared folder name make sure to use the Folder Name as appears when editing the folder through the VirtualBox management program AND NOT THE FOLDER PATH! So if your folder path has spaces in it (e.g. shared folder for vm), your folder name would have underscores instead (e.g. shared_folder_for_vm). Unfortunately there is not indication if you misspell anything past the YOURVMNAME part. But you can check what you entered using:
VBoxManage getextradata YOURVMNAME enumerate
[Source]

Clock skew
Not really an issue for me, but good to know at any rate. [Source]

SSH from host
I couldn't figure out how to copy and paste into the VM through the VirtualBox window so I decided to SSH into it since I can copy and paste into putty. There's a lot of guides for this, but here's a succinct one.

SSH connection lost after sleep
I would lose my connect every time my computer woke up from sleep. The VM would still be running in the background (I run it headless) and new SSH connections could be made, but existing ones would be disconnected and wouldn't be able to reconnect. Apparently this is a known bug. I'm hopeful that the following fixes this:
VBoxManage modifyvm UbuntuServer --natdnshostresolver1 on
[Source]

Sunday 3 April 2016

Removing the birthdays of deleted Skype contacts from your calendar

I'm using Outlook.com's calendar and for whatever reason it insists on showing the birthdays of already deleted Skype contacts. When I try to edit or delete the birthday events I'll get a popup saying either there's been an error or I can't access that calendar right now. This is probably a leftover result of Skype and Windows Live being merged.

I found an answer here, but it was somewhat incomplete. Delete the birthday calendar and then recreate it. When it's recreated it will only list the birthdays of your current Skype and Outlook.com contacts. Unfortunately I couldn't figure out how to recreate the birthday calendar (it's not the same as making a new calendar and calling it "Birthdays"). A little more google-fu turned up these instructions on how to do exactly that.

Wednesday 17 February 2016

Project Euler #5: Smallest multiple

2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.
What is the smallest positive number that is evenly divisible by all of the numbers from 1 to 20?
 There's actually a really easy trick to solving this. It's not immediately obvious and I can't explain how it works well, but I'll do an example. Say we want the least common multiple of 1 through 10 (aka the smallest multiple). What we do is remove the prime factors from each of the numbers in order of smallest to lowest, but when take out a prime factor, we also remove it from any other number it can be removed from. For example
  • 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
  • Remove 2: 1, 1, 3, 2, 5, 3, 7, 4, 9, 5
  • Remove 3: 1, 1, 1, 2, 5, 1, 7, 4, 3, 5
  • Remove 2: 1, 1, 1, 1, 5, 1, 7, 2, 3, 5
  • Remove 5: 1, 1, 1, 1, 1, 1, 7, 2, 3, 1
  • Remove 7: 1, 1, 1, 1, 1, 1, 1, 2, 3, 1
  • Remove 2: 1, 1, 1, 1, 1, 1, 1, 1, 3, 1
  • Remove 3: 1, 1, 1, 1, 1, 1, 1, 1, 1, 1
Now we multiply all the prime factors we extracted and that's our answer! So 2 x 2 x 2 x 3 x 3x 5 x 7 = 2520 as expected. Alternatively you could've also only factored the non-prime numbers, but that would require knowing which numbers are prime - which isn't immediately obvious at high values. You could also eliminate any number which already has a multiple of itself - so 1, 2, 3, 4, and 5 - if you wanted to. An alternate version can be found here.

Coding this was pretty simple and the runtime is under 10 microseconds for numbers 1 through 20. This is probably the fastest solution I can come up with. The brute force method takes about 3 seconds in comparison.

The code can be found here.

Friday 12 February 2016

Order of operations and overflow

Let's say you want 50% of 256 million (as in powers of two though like mebibytes, so 2 ^28). Easy, that's 128 million ( 2^27). One could easily write it out as
unsigned int elements = 256 * 1024 * 1024 * 50 * 0.01
You're multiplying 256 by 2^20 and by 0.5, which again equals 128 million. Not so fast! Because the operations happen from left to right, at one point you'll end up with a number that's over 13 billion (13,421,772,800 to be exact). Unsigned ints only go up to around 4 billion, so you'll overflow. The problem is you get an intermediate large number before your final answer. A safer approach is to be multiply the 0.01 before the 50 so you get a smaller intermediate number before your final answer. Yes, even though this is obvious, I still made this mistake.

Wednesday 10 February 2016

Project Euler #3: Largest prime factor


The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
I started off by recycling the code from PE-10. To find the largest prime factor I'm going to find ALL the prime factors. By finding them in increasing order, I'm guaranteed that the last one I find will be the largest prime factor (side note: this also works if the inputted number is a prime number since the factors are 1 and itself).

I begin by setting the limit to the inputted number and starting at 5 and repeating my earlier method to find prime numbers in increasing order (sieving on 2 and 3, then using the idea that the smallest prime factor must be less than or equal to the square root of the number being tested if it's NOT a prime number). For every prime number I test if it's a factor of the inputted number. If it isn't then I store it in my prime number vector and move on. However, if it is then I set the new limit to be the old limit divided by this prime factor. Then I repeat the process. I continue until I reach the limit.

This works only because I'm finding the prime factors in order. Once we find one, we know the next one must be greater or equal to the one we just found. Thus I can continue on in my for-loop, only having to retest the most recent prime number (e.g. 8 is 2x2x2) before moving on. I like this solution because the amount of numbers we test is quite low so it scales really well.

For example, this problem asked for the prime factors of 600851475143. The prime factors are:
  1. 71, new limit is 8462696833
  2. 839, new limit is 10086647
  3. 1471, new limit is 6857
  4. 6857, new limit is 6857 (since they're equal)
Once the latest prime factor equals the limit we know we're done! So we only had to test roughly ~1142 numbers (6857 / 6 since we sieved). I'm not sure if the runtime I measured using is accurate, but it's returning 502 microseconds. I didn't bother making a naive version for this one since I was lazy.

The code can be found here.

Wednesday 3 February 2016

Project Euler #1: Multiples of 3 and 5

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.

I'm feeling lazy so I'll do a quick post about the first problem since it's pretty straightforward. All I did was  loop through every number from 1 until the limit and test if either 3 or 5 was a divisor. Brute force ftw! Including the printing this takes 0.002 seconds.

Even still, I can think of a few things to speed it up if only trivially.
  1. Use something other than mod
  2. Instead of incrementing by 1, keep two counters - one for 3 and the other for 5. Increment whichever one is lower and add it to the sum until both counters reach the limit. This way you save on having to test if the number is a multiple of 3 or 5. You do however need to check if your two counters are equal. If they are then you skip that number.
The second solution might deserve to be coded for clarity if there's any interest. Be sure to check out the solution thread as there are some nice ways to get the answer without any coding.

Edit: The code is not formatted properly when embedded here for whatever reason

Saturday 30 January 2016

Empty loop not responding to exit condition (also: how to multi-thread on Windows)

I had the following code
HANDLE * threadHandles
bool exitThreads
void threadEntryPoint(void * data)
{
    while (exitThreads == false){}
    _endthreadex(1);
}

void launchThreads(int nThreads)
{
    exitThreads = false;
    threadHandles = new HANDLE[nThreads];
    for (uint32_t i = 0; i < nThreads; ++i) {
        threadHandles[i] =
            (HANDLE)_beginthreadex(0,
            0,
            (uint32_t(__stdcall *)(void*))threadEntryPoint,
            NULL,
            0,
            0);
    }
}

void killThreads(int nThreads)
{
    exitThreads = true;
    for (uint32_t i = 0; i < nThreads; ++i) {
        WaitForSingleObject(threadHandles[i], INFINITE);
        CloseHandle(threadHandles[i]);
    }
    delete[] threadHandles;
}
This code will launch nThreads threads which will loop pointlessly until you call killThreads(). Ignore why I chose beginthreadex() over beginthread(). This is a good way to max out the cores on your CPU.

Conceptually this should work. And yet it didn't. The program was hanging it seemed as there was none of the expected output. Thankfully I had only launched it with 3 threads on a quadcore machine - I suspect launching it with 4 would've caused the machine to hang. The first thing I tried was debug mode. Multi-threading can get tricky, so might as well start by debugging it. Oddly enough it worked as expected in debug mode. That was weird, maybe the threads aren't printing properly? I then polluted my code with a bunch of cout statements with flushes in release mode, but I found that it would get to WaitForSingleObject() and then stop printing. So for whatever reason the threads were not exiting.

A quick google search turned up this beauty. It seems that the compiler tried to optimize the release version by not loading the value of exitThreads every loop iteration and instead assume it's always false. By adding the the keyword volatile before the bool exitThreads declaration, the compiler is forced to have the loop load the value every iteration - because the value is possibly volatile. I was aware of this concept before, but it's not something I deal with often so it's easy to slip the mind.