TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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:


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.

share|improve this question
See also Paths and Linux/Windows slash conventions – Leo Liu Nov 29 '11 at 13:45
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
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
There is also the macro \InputIfFileExists – Alain Matthes Nov 29 '11 at 14:05
up vote 4 down vote accepted

Note, that there is also a \InputIfFileExists:

share|improve this answer
Excellent! That makes the code much more readable as well. – Ricardo Altamirano Nov 30 '11 at 14:34

/ works for both *nix and Windows.

share|improve this answer
Working perfectly, and thank you for the link in your comment as well. It's a great resource. – Ricardo Altamirano Nov 29 '11 at 13:51

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.