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.

This question led to a new feature in a package:
As of version 0.6 currfile supports the requested feature via a new package option and the -recorder compiler switch. For details, see Martin's answer below.

From the documentation I assumed that Martin Scharrer's currfile package would do this. However, while it deals well with tracking the actual file name over \inputs and \includes, the name of the main .tex file is still set to \jobname:

\documentclass{article}
\usepackage{currfile}

\begin{document}
  I was compiled as job \texttt{\jobname} from the file \texttt{\currfilename}.
\end{document}

If saved as test.tex and compiled with pdflatex -jobname bla test.tex the output is:

enter image description here

Even compiling with pdflatex -jobname bla "\input{test.tex}" does not change the output.

share|improve this question
1  
Yep, currfile uses \jobname for the main file. I'm not aware of any other way to find out the real file name (or even its extension), but I'm happy to learn about one if it exists. This is actually a recent feature request for the package. –  Martin Scharrer Oct 16 '11 at 18:19
    
I now figured a way out while answering the question Full path of current file. The feature there can also be used for the main file name. I posted an answer showing this. –  Martin Scharrer May 8 '12 at 8:26
add comment

2 Answers

up vote 16 down vote accepted

With the call

pdflatex -jobname bla "\RequirePackage{currfile}\input{test}"

the macro \currfilename prints test.tex

One has to change the meaning of \input (actually of \@iinput) before executing it.

share|improve this answer
    
Neat trick! Actually I have been thinking the same direction, but tried with \usepackage, which doesn't work before the \documentclass. –  Daniel Oct 16 '11 at 19:26
add comment

You were right that my currfile package should give you the real file name, but there is no way to read the real main file name from TeX directly. Therefore I had to use \jobname.

However, I now found a way to do this by reading the \jobname.fls file produced by the -recorder compiler switch. This functionality is available since v0.6 from 2012/05/06. Note that -recorder is already used by latexmk, so if you are using it there is no extra effort.

To enable this simply load currfile with the realmainfile option. The related abspath option, which gives you absolute paths for all input files, will also enable realmainfile.

% Compile with: (pdf|xe|lua|)latex -recorder filename
% Needs two runs with MiKTeX, one with TeX Live
\documentclass{article}

\usepackage[realmainfile]{currfile}[2012/05/06]

\begin{document}

Jobname: \jobname

Main file (path): \currfilepath

\end{document}

If you don't need the full functionality of currfile (which will update several macros for every input file), then you can use the sub-package currfile-abspath directly:

% Compile with: (pdf|xe|lua|)latex -recorder filename
% Needs two runs with MiKTeX, one with TeX Live
\documentclass{article}

\usepackage{currfile-abspath}

\getmainfile % In preamble or body

\begin{document}

Jobname: \jobname

Main file: \themainfile

\end{document}
share|improve this answer
    
Martin, this is way cool! I have edited the package update into the question. –  Daniel May 8 '12 at 8:21
    
@Daniel: Good stuff. Note that your original MWE still requires the realmainfile option in order to work, and -recorder to be used at least once of course. –  Martin Scharrer May 8 '12 at 8:23
    
what about the case when there is an underscore character in the filename (e.g. file_Chapter_001.tex)? –  reckoner Mar 13 '13 at 13:25
    
@reckoner: In currfile all characters in filenames are handled using the category code "other", so underscores work and will not be taken as the math subscript operator. However, usually you need to use a proper font (like tt) to get them typeset correctly. –  Martin Scharrer Mar 13 '13 at 14:19
add 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.