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.

Background:

I am not sure if this is a bug with the currfile package, or if I not properly using the package, or perhaps an issue with using .fmt files. BTW, this is my first attempt at using .fmt files.

Since I use the same preamble in all my documents I am trying to speed up compilation by generating a .fmt file as per the referenced questions below, but am having trouble with some of the things that are performed \AtBeginDocument. The example below test to see that the value of the token \MyToken is set to the portion of the file name between the two dashes.

Problem:

Using the precompiled .fmt file I get:

enter image description here

Note that the value of \currfilename is the file name of the preamble file, which is not what I want (Perhaps some \expandafter magic can help).

However, if I make one complete .tex file with the two given below (and remove the %& MyPreamble), the output you get is the desired behavior. The value of \currfilename is the name of the file.

enter image description here

Partial Solution

  • One partial solution is to use \jobname (as per the commented line in the preamble). Then this produces identical results in both cases. But, I can't use that in my real application as I change \jobname in order to generate multiple versions from the same source document.

References:

Notes:

  • The actual files names here matter.
  • To generate the MyPreamble.fmt, I use:

    pdflatex -ini -jobname=MyPreamble "&pdflatex MyPreamble.tex\dump"
    

Code: MyPreamble.tex:

\documentclass{article}
\usepackage{xcolor}
\usepackage{xstring}
\usepackage{lipsum}
\usepackage[realmainfile]{currfile}% 

\newtoks{\MyToken}
\MyToken={oo}% set default value
\AtBeginDocument{%
    \par\noindent\textcolor{blue}{Debug: currfilename = "\currfilename"}\par%
    % These two (StrBefore and StrBetween) statements do not work,
    % if this is used via .fmt file.
    \StrBefore{\currfilename}{.}[\CurrentFileName]% 
    \StrBetween[1,2]{\CurrentFileName}{-}{-}[\ExtractedValue]%
    %
    %\StrBetween[1,2]{\jobname}{-}{-}[\ExtractedValue]% this works
    \IfStrEq*{\ExtractedValue}{\the\MyToken}{}{%
        \par\noindent\textcolor{red}{Error: MyToken (middle) was "\the\MyToken", 
                but was expected to be "\ExtractedValue".}\par%
    }%
}
%% ----- preamble ends here

Code: foo-x-bar.tex:

%& MyPreamble

% This file need to be named in three parts separated by two dashes.
% This need to be set to be the value in between the two dashes. So,
% if this file is named "foo-x-bar.tex", then this needs to be "x"
\MyToken={x}%

\begin{document}
    \lipsum[1]
\end{document}
share|improve this question
add comment

2 Answers

up vote 5 down vote accepted

If you use \getmainfile and \themainfile, the correct file name is used:

\documentclass{article}
\usepackage{xcolor}
\usepackage{xstring}
\usepackage{lipsum}
\usepackage[realmainfile]{currfile}% 

\newtoks{\MyToken}
\MyToken={oo}% set default value
\AtBeginDocument{%
    \getmainfile
    \par\noindent\textcolor{blue}{Debug: themainfile = "\themainfile"}\par%
    % These two (StrBefore and StrBetween) statements do not work,
    % if this is used via .fmt file.
    \StrBefore{\themainfile}{.}[\CurrentFileName]% 
    \StrBetween[1,2]{\CurrentFileName}{-}{-}[\ExtractedValue]%
    %
    %\StrBetween[1,2]{\jobname}{-}{-}[\ExtractedValue]% this works
    \IfStrEq*{\ExtractedValue}{\the\MyToken}{}{%
        \par\noindent\textcolor{red}{Error: MyToken (middle) was "\the\MyToken",
                but was expected to be "\ExtractedValue".}\par%
    }%
}
%% ----- preamble ends here

With

pdflatex -recorder foo-x-bar

I get at the first run

enter image description here

share|improve this answer
add comment

When compiling the document file, TeX does not use \input to load it, hence there's nothing for currfile to hook onto, hence why \currfile is not correct. You can work around this by compiling the document file instead with the command line

pdflatex -recorder -fmt MyPreamble '\input{foo-x-bar.tex}'

(Remember when loading the currfile package with the realmainfile option, you should compile with the -recorder option and compile twice.)

share|improve this answer
    
Thanks for this solution. This would have required me to change the compile command for each file, so have accepted the other answer. I was about to switch back to using \jobname and adapt your solution to provide the fix for the case where I could not use jobanme, before egreg came to the rescue. –  Peter Grill Nov 7 '12 at 0:38
    
@PeterGrill: no problem. I too played with \themainfile (and I noticed you were using the realmainfile option to currfile but not using \getmainfile etc) but didn't get a solution as good as egreg's! –  cyberSingularity Nov 7 '12 at 8:52
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.