2

I created some style files for standalone pictures. However, I get unwanted horizontal space.

Here is my first MWE1:

\documentclass[convert={true,convertexe={magick.exe},
   command=\unexpanded{{\convertexe\space -density \density\space \infile\space \ifx\size\empty\else -resize \size\fi\space \outfile}}}]{standalone}

\RequirePackage{forest}

\begin{document}

\begin{forest}
[ 2 [1] [3]]
\end{forest}

\end{document}

The result is:

MWE1

However, I want to change my transparent background. Here is MWE2:

\documentclass[convert={true,convertexe={magick.exe},
   command=\unexpanded{{\convertexe\space -density \density\space \infile\space \ifx\size\empty\else -resize \size\fi\space -transparent white'rgb(100,200,100)' \outfile}}}]{standalone}

\RequirePackage{forest}
\RequirePackage{xcolor}

\begin{document}

\definecolor{tr}{RGB}{100,200,100}
\pagecolor{tr}

\begin{forest}
[ 2 [1] [3]]
\end{forest}

\end{document}

The result is:

MWE2

Notice the extra horizontal space before the tree. This is just a MWE, so it is not a lot. However, with bigger trees, the space gets bigger:

MWE3

Does anyone have an idea to get rid of this?

7
  • 3
    Welcome! Add percentage signs: \definecolor{tr}{RGB}{100,200,100}% and \pagecolor{tr}%. You may also just move \begin{document} after \pagecolor{tr}.
    – user194703
    Commented Oct 24, 2019 at 12:46
  • Which TeX distribution are you using and is it current?
    – cfr
    Commented Oct 26, 2019 at 1:53
  • @Schrödinger'scat Can you test \documentclass{article} \usepackage{forest} \begin{document} \begin{forest} for tree={circle,draw} [1[2[3][4]][5]] \end{forest} \end{document}? Do you get an error?
    – cfr
    Commented Oct 26, 2019 at 1:54
  • 1
    @cfr I do: dimension too large.
    – user194703
    Commented Oct 26, 2019 at 1:58
  • 1
    tex.stackexchange.com/questions/513716/… @Schrödinger'scat <at> anyone.
    – cfr
    Commented Oct 26, 2019 at 2:04

3 Answers 3

4

You just have to place the settings before \begin{document}

\documentclass{standalone}

\usepackage{forest}
\usepackage{xcolor}

\definecolor{tr}{RGB}{100,200,100}
\pagecolor{tr}

\begin{document}

\begin{forest}
[ 2 [1] [3]]
\end{forest}

\end{document}

enter image description here

However, forest has the tendency to add space; an easy workaround is to call standalone with the varwidth option.

\documentclass[varwidth]{standalone}

\usepackage{forest}
\usepackage{xcolor}

\definecolor{tr}{RGB}{100,200,100}
\pagecolor{tr}

\begin{document}

\begin{forest}
  [A[B[D][E]][C[F][G]]]
\end{forest}

\end{document}

enter image description here

Compare with the output one gets without varwidth:

enter image description here

General solution

The spaces are added because of an unprotected end of line in the definition of \pgfutilsolvetwotwoleqfloat in pgfutil-common.tex, precisely at the end of line 712

712                 \edef\pgf@marshal{
713                     \noexpand\pgfmathfloatdivide@
714                         {\csname r\Pb\endcsname}
715                         {\csname m\Pb b\endcsname}%
716                 }%

Indeed, if I do

\documentclass{standalone}

\usepackage{forest}
\usepackage{etoolbox}

\makeatletter
% remove the stray space
\patchcmd{\pgfutilsolvetwotwoleqfloat}
  { \noexpand\pgfmathfloatdivide@}
  {\noexpand\pgfmathfloatdivide@}
  {}{}
\makeatother

\begin{document}

\begin{forest}
  [A[B[D][E]][C[F][G]]]
\end{forest}

\end{document}

the result is correct as expected

enter image description here

Thanks to cfr who was able to circumscribe the problem. See the issue report at https://github.com/pgf-tikz/pgf/issues/764

5
  • 1
    This doesn't solve the general problem which is that space is being added inside the forest environment. It does not happen with all trees, including the one here, but it happens with many (most?). That is, this solves the problem for the first tree, but not the second.
    – cfr
    Commented Oct 29, 2019 at 2:06
  • @cfr Now I saw the issue, with a different forest. The workaround is using varwidth.
    – egreg
    Commented Oct 29, 2019 at 7:52
  • 1
    @cfr Found, it's pgf's fault, not forest's.
    – egreg
    Commented Oct 29, 2019 at 13:33
  • Fantastic! Schrödinger's cat and I were trying to pinpoint it below, but I, at least, was not getting much beyond the intersections library.
    – cfr
    Commented Oct 29, 2019 at 15:48
  • It was a long time ago since I posted this problem. Sorry for not responding. Thank you for your investigation and solution!
    – Eltjo
    Commented Oct 17, 2020 at 11:31
2

This isn't an answer, but an attempt to point towards the problem. forest is adding space, as the following example shows:

\documentclass[border=12pt]{standalone}
\usepackage[]{forest}
\forestset{
  pack stage/.style={TeX=a,for root'=pack,TeX=b}
}
\begin{document}
\fbox{\begin{forest}
  [A[B[D][E]][C[F][G]]]
\end{forest}}
\end{document}

spurious space with Forest

The problem can be traced to the following lines in forest.sty:

  \pgfintersectionofpaths{\pgfsetpath\forest@all@edges}{\pgfsetpath\forest@node@edge}%
  \def\forest@edgenode@intersections{}%

Adding markers as follows

  X\pgfintersectionofpaths{\pgfsetpath\forest@all@edges}{\pgfsetpath\forest@node@edge}%
  Y\def\forest@edgenode@intersections{}%

shows the culprit is somewhere here

culprit between X and Y

However, I'm not sure how to debug this further, so posting this in the hope somebody else may be able to follow the trail from here.

15
  • 1
    Naively one may think it shouldn't come from the \pgfsetpath because it is used all over and does not seem to produce spaces. But this could be wrong since in tikzpictures these spaces would be gobbled anyway. This leads to the question how on earth the problem can arise because I thought a forest tree is a tikzpicture. Does forest have a "survey phase" before it starts the actual tikzpicture?
    – user194703
    Commented Oct 29, 2019 at 2:37
  • 1
    @Schrödinger'scat Yes. At least, I think so. It works in stages and I don't think they're all inside tikzpicture. I've seen Forest add spurious spaces before, but I think this one is at lines 168/169 of pgflibraryintersections.code.tex. At least, if I put markers on those lines, I catch the spaces between.
    – cfr
    Commented Oct 29, 2019 at 2:46
  • 1
    Yes. I think to have ruled out \pgfsetpath , which is northing but \pgfsyssoftpath@setcurrentpath which gets defined in pgfsyssoftpath.code.tex, and seems to be clean.
    – user194703
    Commented Oct 29, 2019 at 2:48
  • 1
    @Schrödinger'scat In fact, you can change the tikzpicture environment to something else, if you want to. It is only used in the final stage, when the tree is actually drawn (or saved into a box, if you choose that option). Before that, we have all the other stages. prooftrees has even more :(.
    – cfr
    Commented Oct 29, 2019 at 2:53
  • 1
    I was hoping that the old pgfplotsoldpgfsupp_pgflibraryintersections.code.tex would not have the problem, but no. So one real possibility is that in the very tiny ecosystem of TikZ somewhere there is a spurious space that went mostly unnoticed so far because the respective macro was mostly used inside a tikzpicture environment, where it does not have an effect. Searching for a needle in a haystack might be fun in comparison... :-(
    – user194703
    Commented Oct 29, 2019 at 3:05
0

I want to add an answer for anyone with a recent TeXlive (in my case 2022) who comes across this question and still gets horizontal space. Use case is for defining commands for that standalone file that are not needed in the main document that the standalone is being \includestandalone{}'ed into to. Comments are required at the end of every line after \begin{document} regardless of whether it is necessary in normal LaTeX. For example:

\documentclass{standalone}

\usepackage{forest}
\usepackage{xcolor}

\begin{document}
\definecolor{tr}{RGB}{100,200,100}
\pagecolor{tr}
\tikzset{%
    dotted_block/.style={draw=red!70!white, line width=0.5pt, dash pattern=on 2pt off 2pt,
            inner ysep=0pt,inner xsep=-2pt, rectangle, rounded corners}%
}
\begin{forest}
[ 2 [1] [3]]
\end{forest}
\end{document}

produces:

enter image description here

whereas with comments at the end of every line after \begin{document}:

\documentclass{standalone}

\usepackage{forest}
\usepackage{xcolor}

\begin{document}
\definecolor{tr}{RGB}{100,200,100}%
\pagecolor{tr}%
\tikzset{%
    dotted_block/.style={draw=red!70!white, line width=0.5pt, dash pattern=on 2pt off 2pt,%
            inner ysep=0pt,inner xsep=-2pt, rectangle, rounded corners}%
}%
\begin{forest}
[ 2 [1] [3]]
\end{forest}
\end{document}

produces:

enter image description here

I could not find a reference to this and searching within the forest documentation on CTAN for "space" and "comment" didn't turn up this advice so maybe it will be helpful to others.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .