# TeXworks and Command Line Compilation ok, But TeXShop yields different result

## 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):

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

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:

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 http://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:

-
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

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.

-