6
\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.

5
  • 3
    Try \@currname, maybe? Mar 11, 2011 at 13:30
  • @Bruno That works! Thanks. Could you make that an answer?
    – Seamus
    Mar 11, 2011 at 13:36
  • @Bruno actually, it doesn't I got confused.
    – Seamus
    Mar 11, 2011 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. Mar 11, 2011 at 13:59
  • @Martin: I did say "maybe" ;-). Thanks. Mar 11, 2011 at 14:24

1 Answer 1

9

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}
6
  • 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. Mar 11, 2011 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, 2011 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. Mar 11, 2011 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, 2011 at 15:25
  • @Seamus: Either compile the .ins file to convert the .dtx to .sty or install(=unpack) the TDS ZIP file instead. Mar 11, 2011 at 15:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .