# Path expansion in LaTeX

On Linux, your home directory is typically /home/username; on OS X, it’s /Users/username. If you work on different operating systems, or if you have different usernames on different computers, and you want certain things to work the same way across all your machines, these differences can lead to small annoyances. Recently, I learned a way to eliminate one of those annoyances when working in LaTeX.

On both of my computers, I have a PDF of my signature stored in a file called, cleverly enough, signature.pdf. It’s in the same subdirectory of my home directory on both machines, but because I use different usernames on these machines,1 it’s /Users/name/graphics/signature.pdf on my office computer and /Users/drdrang/graphics/signature.pdf on my notebook.

In shell scripts that access the signature file, this difference poses no problems. The file can be addressed as ~/graphics/signature.pdf on both computers, because the tilde expands to the home directory. Similarly, in Python scripts, it’s os.env['HOME']/graphics/signature.pdf on both computers.

The problem is in LaTeX files. I have a signature macro defined this way on my MacBook Air:

\signature{\vspace{-.625in}\hspace{-.125in}\includegraphics{/Users/drdrang/graphics/signature.pdf}\\
\vspace{-.125in}My Real Name}


Because it uses an absolute path, it has to be defined slightly differently on my iMac at the office. It would be nice if I could define it the same way on both machines, but

\signature{\vspace{-.625in}\hspace{-.125in}\includegraphics{~/graphics/signature.pdf}\\
\vspace{-.125in}My Real Name}


doesn’t work because the tilde has special meaning (nonbreaking space) in LaTeX. You might think escaping the tilde with a backslash (\~) would fix the problem, but it doesn’t.

The solution, which I found in an answer to this Stack Exchange question, is to use the primitive \string command to get the underlying TeX engine to treat the tilde literally rather than as a special character. My signature command is now

\signature{\vspace{-.625in}\hspace{-.125in}\includegraphics{\string~/graphics/signature.pdf}\\
\vspace{-.125in}My Real Name}


Kpathsea, the system TeX and LaTeX use for searching paths, understands the tilde, so now I have just one signature definition on both computers.

Similarly, the LaTeX code for other graphic elements that I commonly use in reports—the company logo, for example—has been unified across machines, and I no longer have to keep two slightly different versions in sync with one another.

1. Why have different usernames? Mainly because I do all my blogging from my MacBook Air, and it’s generally easier to maintain my pseudonymity if my username is drdrang on that machine. On my iMac at the office, it’s easier—and less weird looking—to have my real name as my username. Of course, I often do real work from my MacBook Air, and that’s where the annoyance described in this post arises.