Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.
\begin{filecontents}{foo.tex}
\def\foo{\jobname}
\end{filecontents}
\documentclass{article}

\begin{document}
\input{foo}
\foo
\end{document}

This outputs the jobname of the main file not "foo". How can I define a macro that outputs the jobname of the inputted file?

\edef doesn't work either.

share|improve this question
3  
Try \@currname, maybe? –  Bruno Le Floch Mar 11 '11 at 13:30
    
@Bruno That works! Thanks. Could you make that an answer? –  Seamus Mar 11 '11 at 13:36
    
@Bruno actually, it doesn't I got confused. –  Seamus Mar 11 '11 at 13:37
2  
@Bruno, @Seamus: \@currname is only valid inside packages or classes and holds the name of this package or class. It is empty otherwise. –  Martin Scharrer Mar 11 '11 at 13:59
    
@Martin: I did say "maybe" ;-). Thanks. –  Bruno Le Floch Mar 11 '11 at 14:24
add comment

1 Answer

up vote 7 down vote accepted

Use the package currfile, which keeps track of the file name of the input file in the macros

\currfiledir
\currfilebase
\currfileext
\currfilename
\currfilepath

Furthermore, \edef has to be used to expand these values within the input file, not when you are back in the main file:

\begin{filecontents}{foo.tex}
\edef\foo{\currfilebase}
\end{filecontents}
\documentclass{article}
\usepackage{currfile}
\begin{document}
\input{foo}
\foo
\end{document}
share|improve this answer
3  
Nice answer. @Seamus: It is also possible to do \let\foo\currfilebase. Note that all the mentioned macros hold the corresponding strings directly without expanding to further macros, e.g. \currfilepath doesn't call \currfiledir\currfilename. So using \expandafter on them will expand the string directly. See also the filehook package used by currfile and written be the same author (me), which allows you to execute such commands for every input file automatically. –  Martin Scharrer Mar 11 '11 at 14:04
    
@Martin Thanks! looks like a good package. Can I take this opportunity to admonish you for not having a "how to install" in your README (I know it's a simple .dtx but I still think you should have a boilerplate "how to install a dtx" text in your readme...) –  Seamus Mar 11 '11 at 15:18
    
@Seamus: It is part of TeXLive and MikTeX, so no manual install is required. I think I put some INSTALL section into my first packages, but stopped with this habit. I will add some info. –  Martin Scharrer Mar 11 '11 at 15:21
    
@Martin unfortunately it is not in the ubuntu version of TL09. I really need to get rid of that and install 2010 myself... –  Seamus Mar 11 '11 at 15:25
    
@Seamus: Either compile the .ins file to convert the .dtx to .sty or install(=unpack) the TDS ZIP file instead. –  Martin Scharrer Mar 11 '11 at 15:33
show 1 more comment

Your Answer

 
discard

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.