5

I'm trying to use a command in my LaTeX file that inputs a file if it exists, and if it doesn't exit, inputs another one. This is the command:

\IfFileExists{/media/kingstonGB/LaTeX/preamble}{\input{/media/kingstonGB/LaTeX/preamble}}{\input{E:\\LaTeX\\preamble}}

In short, the command searches for the file in /media/kinstgonGB (which should succeed on my unix systems, and if it finds it, inputs it. Otherwise, if that file is not found, it means the LaTeX is being compiled on a Windows system, so it should look for files in E:\. However, I get this error message:

! LaTeX Error: Missing \begin{document}.

I've also tried wrapping E:\LaTeX\preamble in quotes, or escaping it (as done here), or both, and the error is the same.

lockstep
  • 250,273
  • 3
  • 1
    I think you may be abusing the \IfFileExists command: What you're looking for should be a file, but the command's first argument looks like a path to a file, not a file itself. Or is there maybe a file named preamble.tex? If so, it might be a good idea to add the .tex extension explicitly and try recompiling. Separately, you may want to add a second \IfFileExists instruction, in the second argument of the first such command, to check explicitly for the existence of the file e:\\LaTeX\\preamble.tex -- unless you're always good and never forget to insert the USB key. :-) – Mico Nov 29 '11 at 13:52
  • There is a file named preamble.tex. I use a standard preamble for all of my LaTeX documents, so I normally input it in the header. I don't need to specify the .tex extension with the \input command, though, right? – Ricardo Altamirano Nov 29 '11 at 13:54
  • 1
    No, it's not necessary to add the extension .tex, but doing so wouldn't hurt the code's legibility either. :-) Re-check the error message you're getting: is it being generated by the \IfFileExists command, or is it maybe being generated by some instruction contained in preamble.tex? For instance, do you get the same error message if the file preamble.tex is in your current working directory and you load it from the main driver file with the command \input preamble? – Mico Nov 29 '11 at 13:56
  • I added the .tex extension for both the IfFileExists and the two \input commands, and on Windows, it's still working properly (I'll test on *nix when I'm back at the flat). Thank you! – Ricardo Altamirano Nov 29 '11 at 13:58
  • 2
    There is also the macro \InputIfFileExists – Alain Matthes Nov 29 '11 at 14:05

2 Answers2

6

Note, that there is also a \InputIfFileExists:

\InputIfFileExists{/media/kingstonGB/LaTeX/preamble}{}{
  \input{E:/LaTeX/preamble}%
}
Schweinebacke
  • 26,336
5

/ works for both *nix and Windows.

\input{E:/LaTeX/preamble}
Leo Liu
  • 77,365