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
:
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
Using TeXworks (works):
With
MyLaTeX
configured asI get identical results as command line compilation (again, no red error message).
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
fromTeXShop
, I get the dreaded red error message:Compiling the
foo-x-barComplete.tex
, which is simply:\input{MyPreamble.tex} \input{foo-x-bar.tex}
directly via
LaTeX
onTeXShop
, andpfdlatex
onTeXworks
, 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
andTeXworks
, 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}
./../Folder/./././../././Folder/file.tex
is a valid name of a file in the current folder, as long as it's name isFolder
;).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 thebash
script would pass the file name topfdlatex
without the leading./
then it might just work..../
withbfname=$(dirname "$1")/"`basename "$1" .tex`"
Try withbfname=$(basename "$1".tex)
or, better yet,bfname="$1"
(I don't see why extracting what's before the extension and adding the extension again).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]
.