TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top


As per my earlier question, Can't get real currfile if used in precompiled .fmt file, I am trying to speed up compilation by generating a .fmt file. The example below tests that the value of the token \MyToken is set to the portion of the file name between the two dashes.

I am not getting the desired behavior when I use TeXShop and the precompiled .fmt file. But, things work as expected if I use TeXworks, or command line compilation. I suspect something is not quite right with the MyLaTeX.engine script invoked by TeXShop, but don't know for sure.

After saving MyPreamble.tex defined below, I first compile the preamble via the command line:

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

Then I try to use this preamble in four different ways to process foo-x-bar.tex:

  1. Command Line compilation (works):

    pdflatex  --file-line-error --shell-escape -fmt=MyPreamble -recorder --synctex=1 \

    yields the correct result (no red error message):

    enter image description here

    Furthermore, the following (change how the file name is specified, see the "Update" section below) also works:

    pdflatex  --file-line-error --shell-escape -fmt=MyPreamble -recorder --synctex=1 \
  2. Using TeXworks (works):

    With MyLaTeX configured as

    enter image description here

    I get identical results as command line compilation (again, no red error message).

  3. Using TeXShop (does not work):

    With TeXShop things are not quite the same. I have the following file saved as MyLaTeX.engine:

    bfname=$(dirname "$1")/"`basename "$1" .tex`"
    pdflatex --file-line-error --shell-escape -fmt=MyPreamble -recorder --synctex=1 "$bfname"

    and invoking MyLaTeX from TeXShop, I get the dreaded red error message:

    enter image description here

  4. Compiling the foo-x-barComplete.tex, which is simply:


    directly via LaTeX on TeXShop, and pfdlatex on TeXworks, things also work fine (no red error message).

So, the problem only occurs on TeXShop with the precompiled .fmt file.


  • After posting the question, I do see one difference in the TeXShop run in that the file name is prefixed with ./. And I am extracting the text between the first and second dash with:


    This seemed to be the problem (since there are now two dots in the file name). So, I attempted the obvious fix which is to extract the file name before .tex:


    Now I get identical results with both TeXShop and TeXworks, but now both show the dreaded error messages!!!

    Ok, so the fix lies in this code, or the .engine file.

    However it should be noted that the command line invocation with a leading ./ before the file name does not have a problem.

Temporary Solution:

Found a temporary solution:


but I would prefer to find a method that extracts this from the file name with the extension removed, which is what the above code attempts to do.

Code: MyPreamble.tex:

% Based on egreg's answer at http://tex.stackexchange.com/questions/80453/cant-get-real-currfile-if-used-in-precompiled-fmt-file


\MyToken={oo}% set default value
    \Debug{Debug: themainfile = "\themainfile"}%
    \Debug{Debug: CurrentFileName = "\CurrentFileName"}%
    \Debug{Debug: ExtractedValue = "\ExtractedValue"}%
    %\StrBetween[1,2]{\jobname}{-}{-}[\ExtractedValue]% this works
        \Error{Error: MyToken (middle) was "\the\MyToken",
                but was expected to be "\ExtractedValue".}%
%% ----- preamble ends here

Code: foo-x-bar.tex:

% 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"



share|improve this question
File name manipulation is always tricky! Remember that ./../Folder/./././../././Folder/file.tex is a valid name of a file in the current folder, as long as it's name is Folder ;) – yo' Nov 12 '12 at 8:08
@tohecz: Well, yeah, but that is why I thought getting the string before .tex instead of before the . should have worked. I am not looking for a general solution that will cover all wacky cases of file names. So, I am thinking that if perhaps the bash script would pass the file name to pfdlatex without the leading ./ then it might just work... – Peter Grill Nov 12 '12 at 8:14
You are adding the ./ with bfname=$(dirname "$1")/"`basename "$1" .tex`" Try with bfname=$(basename "$1".tex) or, better yet, bfname="$1" (I don't see why extracting what's before the extension and adding the extension again). – egreg Nov 12 '12 at 21:37
@egreg: That certainly does the job with TeXShop, but only if I use \StrBefore{\themainfile}{.}[\CurrentFileName], which is what I was originally doing since there was only one . to deal with, and your suggestion here does. So, this seems like an answer. As an aside, why does it fail with if I instead use \StrBefore{\themainfile}{.tex}[\CurrentFileName]. – Peter Grill Nov 12 '12 at 21:45
@egreg: You should add that as answer so we can mark this as being answered. – Peter Grill Jan 21 '13 at 23:30
up vote 2 down vote accepted

When the file is in the same directory, which is the case here,

$(dirname "$1")

returns .; so

bfname=$(dirname "$1")/"`basename "$1" .tex`"

when the file is foo-x-bar.tex stores


as the value of bfname. Just use


(or use directly $1, of course). Indeed with

bfname="`basename "$1" .tex`"

you'd be removing the extension just to reinsert it afterwards.

share|improve this answer

Your Answer


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.