1

I am working with standalone sub-file in build mode. I would like to pass lengths from the main file to the sub-file. To do so, according to this answer , I put it in the command used by standalone to create the PDF file.

But I am surprised by the different behaviour of some lengths in this command.

To illustrate my problem, here is a MWE: subfile.tex:

\documentclass{standalone} 


\begin{document}
\begin{tabular}{p{\linewidth}}

    textheight:\the\textheight textwidth:\the\textwidth \ line:\the\linewidth and the baseline: \the\baselineskip \\
 An other line.

\end{tabular}
\end{document}

the main.tex:

\documentclass{article}

\usepackage{standalone}             %
    \standaloneconfig{build={command={\latex\space\latexoptions\space\quote\commandStandaloneBuild\string\input{\file}\quote} }
        }

    \makeatletter
    \newcommand{\commandStandaloneBuild}{%
        \string\AtBeginDocument\string{
            \string\setlength\string{\string\textwidth\string}\string{\the\textwidth\string}
            \string\setlength\string{\string\textheight\string}\string{\the\textheight\string}
            \string\setlength\string{\string\linewidth\string}\string{\the\linewidth\string}
            \string\setlength\string{\string\baselineskip\string}\string{\the\baselineskip\string}
        \string}
    }   
    \makeatother


\begin{document}

    \setlength{\baselineskip}{30pt} 
baseline: \the\baselineskip
\setlength{\linewidth}{200pt} 
the line :\the\linewidth
\setlength{\textwidth}{450pt}
the textwidth :\the\textwidth
\setlength{\textheight}{500pt}
the textheight :\the\textheight


    \begin{figure}[h]
        \centering
        \includestandalone[mode=build,
            ]{subfile}
        \caption{test}
        \label{lab}
    \end{figure}

now the command is :\commandStandaloneBuild
\end{document}

In this case, results from the standalone built is a linewidth of 345pt and a baseline of 12pt, while it should be respectively 200pt and 30pt. But the textwidth and textheight are correct. Thus, I checked the main.log and I found:

runsystem(pdflatex -interaction=batchmode -shell-escape -jobname 'subfile' '\AtBeginDocument{ \setlength{\textwidth}{450.0pt} \setlength{\textheight}{500.0pt}\setlength{\linewidth}{345.0pt}\setlength{\baselineskip}{12.0pt} } \input{subfile}')...executed.

Therefore, the information given by the subfile are those given by the build command, but the command 'printed' in the main is not the one passed to the build of standalone.

So, in order to solve my problem I have two related questions: How to make the build command behave the same way for all the length? And why this differences ?

  • if you set \linewidth at begin document to anything other than the value it has (which is \textwidth for single column documents, latex is going to get very confused. What is your intention of setting this (which is never intended to be set by hand in this way)? similarly \begin{tabular}{p{\linewidth}} seems an odd construct as the table will be \linewidth+2\tabcolsep wide, and it is indendent by \parindent so will not fit on the line. – David Carlisle Jul 7 '18 at 21:01
  • @DavidCarlisle, I completely agree about the fact that my tabular will not fit the line, the purpose is not here. It was just to see the possible change of the \baselineskip length, because without tabular there is no line-break in the standalone class. Moreover, I do it this way because the class options preview, varwidth or multi will affect some lengths. And my intention is to test the limit of the method, with these unusual situations and settings. – R. N Jul 7 '18 at 21:28
  • \baselineskip, \linewidth etc are set at begin document based on other settings in the preamble setting \linewidth is never the right thing to do, I would suggest an alternative but I can not guess the intention here – David Carlisle Jul 7 '18 at 21:30
  • @DavidCarlisle, This is just a way to highlight my problem, not a final purpose. Even if, I put these settings in the preamble or even remove it, the evoked problem remains. – R. N Jul 7 '18 at 21:45
  • 1
    sorry but \linewidth is an internal latex parameter that latex sets all over the place and keeps \textwidth, \linewidth, \@totaleftmargin\ and \hsize in correct relationship. I cannot work out the intention here other than you set \linewidth in one place and it has a different value somewhere else, but the solution for that is always not to set \linewidth. Sorry if this sounds unhelpful but I really can't guess the intention but setting \linewidth can never do anything useful and the fact that latex resets it to a different value is expected. – David Carlisle Jul 7 '18 at 21:48
0

As David Carlisle and Ulrike Fischer mentioned, the variables \linewidth and \baselineskip are internal latex parameters. So they have to be set before the beginning of the document when latex compute them.

Thus, a easy work-around is to create local temporary length in the preambule.

% Define two new length to store the local values of linewidth and baselineskip.
\newlength{\locallinewidth}
\newlength{\localbaselineskip}

To update, these values a newcommand is needed.

% Update the local values.
\newcommand{\UpdateLineAndBaseLength}{
    \setlength{\localbaselineskip}{\the\baselineskip}
    \setlength{\locallinewidth}{\the\linewidth}
}

Therefore, we can change the \commandStandaloneBuild to incorporate the new variable.

\string\setlength\string{\string\linewidth\string}\string{\the\locallinewidth\string}
\string\setlength\string{\string\baselineskip\string}\string{\the\localbaselineskip\string}

Then a call of the new command before the \begin{figure} solves the problem.

\UpdateLineAndBaseLength{}
\begin{figure}[h]
    \centering
    \includestandalone[mode=build,
        ]{subfile}
    \caption{test}
    \label{lab}
\end{figure}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.