# How to avoid automatic .tex extension in filenames?

I am in an mildly unfortunate situation in which, because of the way I prepared the files automatically, I ended up with a file to be attached but also another file with the same name but ending in the .tex extension. It seems that when referring to a file inside TeX the file with the extension .tex is always preferred. For example:

If I have the files example.cpp.tex and example.cpp (no .tex extension), then with

\documentclass{article}
\usepackage{attachfile}
\begin{document}
\end{document}


attachfile will attach example.cpp.tex instead of example.cpp. This seems to be also true for many LaTeX commands, such as \input, \include, embedfile etc.

How can I enforce to use the file specified and not the .tex terminated one in general or in particular for \attachfile?

(Note: the problem is not the double extension; the unexpected result will also happen with the files example and example.tex.)

• The TeXboot mentions: In most installations of TeX, the extension .tex will be appended to the file name, as with \input, if no extension is given explicitly. But this shouldn't affect file names with extensions… Aug 3, 2011 at 6:38
• I would recommend something like [untested] rename 's/\.cpp\.tex/-cpp.tex/' in a unix terminal. Much better than trying to fix this through tex Aug 3, 2011 at 10:13
• @Andrey: thanks for pointing that out. I would say that it makes some sense for "\input". But for attachfile? what for, why would it add the .tex extension?
– alfC
Aug 3, 2011 at 18:53
• (many years later) For LuaTeX you can just read the file from Lua. Nov 15, 2021 at 4:44
• @user202729 what do you mean? How?
– alfC
Nov 15, 2021 at 18:10

There is as far as I know no way to get around the problem.

How TeX handles files with unusual numbers of dots (where "unusual" is any number unequal to 1) is quite inpredictable and also has changed over time. In miktex 2.4 e.g. \input{file.cfg} would have tried at first file.cfg then file.cfg.tex, in miktex 2.5 it tried only file.cfg, in the current 2.9 it tries at first file.cfg.tex and then file.cfg.

The only advice I can give: Avoid in TeX multiple dots like the plague.

• I agree, however the real issue IMHO are not the multiple dots but having a file foo.bar and also foo.bar.tex. This constellation should really be avoided. For me TeX Live 2011 always takes foo.bar.tex before foo.bar, even with the low level \@@input primitive. Aug 3, 2011 at 7:56
• The ambiguity between foo.bar and foo.bar.tex only exists because of the multiple dots in one of the file names. If all your files use only one dot the problem can't happen. Aug 3, 2011 at 8:42
• @ulrike's comment: between foo and foo.tex will also prefer foo.tex with \attachfile{foo}, so the double extension is not the problem.
– alfC
Aug 3, 2011 at 18:47
• I've had some luck with appending a . for example I have a file called readme.tex that inputs a file called readme using the input command \input{readme.} (Note the dot). This works in MikTeX 2.9 on Windows 10, I can't vouch for other flavours/distributions though. I think it stems from the need to append a dot when creating .extention files e.g. one must name the file .extention. for it to be called .extention, windows strips the dot. Jul 22, 2016 at 18:29

If these files are generated automatically, then I recommend some regex-fu to disambiguate the files. Obviously, depending on exactly what the automatic naming scheme for the files is, you may need to change the pattern, but this should be pretty general.

rename 's/\.tex/-tex.tex/' *.tex


What this will do is rename each example.tex file to example-tex.tex. Now doing \input{example} will input example, not example-tex.tex.

OK. So this is a little ugly, but it works. If the files are actually like example.cpp.tex then the less ugly renaming would be:

rename 's/\.cpp\.tex/-cpp.tex/' *.tex


This requires a unix-like terminal or something like cygwin. Hints in comments for a native windows equivalent regex batch file renamer appreciated.

• thanks, but the filenames are really automatic I didn't want to change that just because some avoidable issue in LaTeX. I was looking for a latex answer to a latex problem. thanks anyway.
– alfC
Aug 3, 2011 at 18:51
• @alfC but if they're automatically generated, why not just generate them with different names? Or rename them in a predictable way? And given that the issue doesn't seem to be avoidable (see the accepted answer) then what other option do you have? Aug 4, 2011 at 10:47
• in the light of these answers... it seems that none other unfortunately. +1
– alfC
Aug 5, 2011 at 6:02