[Tex/LaTex] TeXworks and Command Line Compilation ok, But TeXShop yields different result

compilingformat-filestexshoptexworks

Background:

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                   foo-x-bar.tex
    

    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                    ./foo-x-bar.tex
    
  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:

    #!/bin/sh
    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:

    \input{MyPreamble.tex}
    \input{foo-x-bar.tex}
    

    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.


Update:

  • 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:

    \StrBefore{\themainfile}{.}[\CurrentFileName]%
    \StrBetween[1,2]{\CurrentFileName}{-}{-}[\ExtractedValue]
    

    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:

    \StrBefore{\themainfile}{.tex}[\CurrentFileName]%
    

    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:

    \StrBetween[1,2]{\themainfile}{-}{-}[\ExtractedValue]%

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 https://tex.stackexchange.com/questions/80453/cant-get-real-currfile-if-used-in-precompiled-fmt-file
\documentclass{article}
\usepackage{xcolor}
\usepackage{xstring}
\usepackage{lipsum}
\usepackage[realmainfile]{currfile}% 

\newcommand*{\Debug}[1]{\par\noindent\textcolor{blue}{#1}\par}%
\newcommand*{\Error}[1]{\par\noindent\textcolor{red}{#1}\par}%

\newtoks{\MyToken}
\MyToken={oo}% set default value
\AtBeginDocument{%
    \getmainfile
    \Debug{Debug: themainfile = "\themainfile"}%
    \StrBefore{\themainfile}{.}[\CurrentFileName]% 
    \Debug{Debug: CurrentFileName = "\CurrentFileName"}%
    \StrBetween[1,2]{\CurrentFileName}{-}{-}[\ExtractedValue]%
    \Debug{Debug: ExtractedValue = "\ExtractedValue"}%
    %
    %\StrBetween[1,2]{\jobname}{-}{-}[\ExtractedValue]% this works
    \IfStrEq*{\ExtractedValue}{\the\MyToken}{}{%
        \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"
\MyToken={x}%

\begin{document}
    \lipsum[1]
\end{document}

Code:

Best Answer

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

./foo-x-bar.tex

as the value of bfname. Just use

bfname="$1"

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

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

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