2

Was: Document starts with dimension as text

I'm using pgfgantt in a file in a subdirectory, which is included using the standalone subpreambles feature. However, the textual value of a dimension is somehow being inserted right at the start of my document, and the document is failing to compile

main.tex:

\documentclass{report}
\usepackage[subpreambles=true]{standalone}

\begin{document}

Before

\input{main/gantt.tex}

\end{document}

main/gantt.tex:

\documentclass{standalone}
\usepackage{pgfgantt}

\begin{document}

\begin{ganttchart}[ vgrid={*1{dotted}}, x unit=4mm, y unit chart=8mm, y unit title=4mm, time slot format=isodate-yearmonth, time slot unit=month, title height=1 ]{2020-06}{2022-12} \gantttitlecalendar{year} \end{ganttchart}

\end{document}

Result of building main.tex:

l.3 \gtt@chartextrasize
                       {0}{0.4pt}
The control sequence at the end of the top line
of your error message was never \def'ed. If you have
misspelled it (e.g., `\hobx'), type `I' and the correct
spelling (e.g., `I\hbox'). Otherwise just continue,
and I'll forget about whatever was undefined.

Overleaf's preview:

enter image description here

Why is 00.4pt appearing in the output? Where did it come from?


Edit: as pointed out in the comments, this failed to compile.

If I add \usepackage{pgfgantt} to main.tex, then the error and the dimension go away.

Why is this necessary?

Eric
  • 669
  • Are you sure your main.tex compiles without error? Because Overleaf is setup to always give a PDF even if there are compilation errors. But in order to do that LaTeX have to make guesses when there are compilation errors. Those choices may give strange results. – daleif Aug 11 '20 at 17:28
  • 1
    Also known as your setup does not compile at my end when I use TeXLive 2020 and a setup like yours. Never ignore compilation errors. The error I get is ! Undefined control sequence \gtt@chartextrasize{0 }{0.39998pt} – daleif Aug 11 '20 at 17:28
  • don't you need [time slot format =isodate-yearmonth] if you use such dates? – Ulrike Fischer Aug 11 '20 at 17:29
  • Looks like I over-mwe'd this, will add back the lost texts – Eric Aug 11 '20 at 17:30
  • Alright, updated with a less reduced example and the error message - thanks! – Eric Aug 11 '20 at 17:34
  • It looks like subpreambles isn't actually working, as adding \usepackage{pgfgantt} to the main document fixes it. – Eric Aug 11 '20 at 17:34
  • 2
    it does work, but not well enough. The code is executed after the aux file has been read and this is too late. – Ulrike Fischer Aug 11 '20 at 17:49
  • Just came to the same conclusion. So is this a bug in standalone? – Eric Aug 11 '20 at 17:50

1 Answers1

3

As noted in the comments this appears to be a bug in standalone. The class documentation mentions (page 23):

The packages are loaded at the end of the preamble using the \AtBeginDocument hook.

This part of the manual is about the sort option, but the remark about loading packages applies also to the subpreambles option without sort.

This can be seen in the package code (around the middle of the file):

\AtBeginDocument{%
  \let\subpreamble\@gobble
  \let\endsubpreamble\relax
  \let\standalonepreambles\relax
  \let\endstandalonepreambles\relax
  \ifsa@sortsubpreambles
    \let\sa@orig@usepackage\usepackage
    \let\usepackage\sa@usepackagewithoutoptions
  \fi
  \InputIfFileExists{\jobname.sta}{}{}%
  \ifsa@sortsubpreambles
    \let\usepackage\sa@orig@usepackage
  \fi
  \immediate\openout\sa@out=\jobname.sta\relax
  \immediate\write\sa@out{\string\standalonepreambles}%
}

However, \AtBeginDocument is executed after the .aux file is read (see for example When is the aux file read and written?). To execute the code at the right time you can use \AtEndPreamble from the etoolbox package. From the manual (page 4):

\AtEndPreamble{<code>} This hook differs from \AtBeginDocument in that the <code> is executed right at the end of the preamble, before the main aux file (as written on the previous LaTeX pass) is read and prior to any \AtBeginDocument code.

You can check that this can indeed be used to address the issue by copying standalone.sty to the same directory as your document, adding \RequirePackage{etoolbox} to standalone.sty, and changing the mentioned occurrence of \AtBeginDocument to \AtEndPreamble.

It would be useful to add this (or an equivalent fix) to standalone.1

1I was thinking about adding an issue to https://bitbucket.org/martin_scharrer/standalone/issues but the Bitbucket issue tracker is read-only at the moment.

Marijn
  • 37,699