# Placing text, includegraphics or a table above and below Tikz picture in standalone

The documents I am working with produce very large Tikz pictures (using Forest package) that span multiple horizontal and vertical pages in PDF.

These pictures need to have titles, compbined of text, regular JPG images (logos) and PST pictures (QR code).

When using standalone class, Forest is produced fine, but any text/tables prior to it appear on its left. Using varwidth parameter to standalone I can put them above Forest, but it will no longer span multiple pages.

How can I get the best of both worlds: put the title above Forest and let it take as much space as it needs at the same time? Also need to put copyright notice below the Forest.

That must have something to do with the calculation of \textwidth but what?

\documentclass[varwidth=true,border=10pt]{standalone}
%\documentclass[border=10pt]{standalone}
\usepackage{forest}
\usepackage{adjustbox}
\usepackage{pst-barcode}
\usepackage[crop=off]{auto-pst-pdf}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}

\begin{document}

\begin{tabular*}{\textwidth}{@{}l @{\extracolsep{\fill}} r@{}}
\begin{tabular}[t]{@{}l@{}}
Title\\ Text
\end{tabular} &
\begin{adjustbox}{valign=t}
\begin{pspicture}(0.7in,0.7in)
\psbarcode{http://www.ctan.org}{}{qrcode}
\end{pspicture}
\end{adjustbox}
\end{tabular*}

\begin{forest}
for tree={
draw=black, align=center, l sep=4ex, parent anchor=south, child anchor=north,
node options={font=\footnotesize, minimum width=14em, minimum height=10ex, rounded corners=1ex},
edge path={
\noexpand\path[\forestoption{edge}]
(!u.parent anchor) -- +(0,-2ex) -| (.child anchor)\forestoption{edge label};
},
where n children=0{
child anchor=west,
edge path={
\noexpand\path[\forestoption{edge}]
([xshift=-6em]!u.parent anchor) |- (.child anchor)\forestoption{edge label};
},
draw=red,for parent={
parent anchor=south, l sep=-12em, grow'=east, calign=child edge, draw=blue
}
}{}
}
[Parent,name=Parent
[Child1
[,phantom]
[Child11]
[Child12]
]
[Child3
[,phantom]
[Child31]
[Child32]
]
[Child3
[,phantom]
[Child31]
[Child32]
]
[Child3
[,phantom]
[Child31]
[Child32]
]
[Child3
[,phantom]
[Child31]
[Child32]
]
[Child3
[,phantom]
[Child31]
[Child32]
]
[Child3
[,phantom]
[Child31]
[Child32]
]
[Child3
[,phantom]
[Child31]
[Child32]
]
]
% <===== Insert below snippet with \node here =====
\end{forest}
\begin{tiny}Copyright \textcopyright Myself, 2014\end{tiny}
\end{document}


If Child1 and Child3 nodes were left, it would render just fine, but including the duplicates of Child3 clips the Forest.

Edit:

One of the workarounds I can think of would be including the necessary content into a \node inside the forest. But when I am trying to move the tabular* environment into a node as follows and to a line indicated by a comment above, it does not seem to be able to calculate the total width of the forest correctly:

\node(title) at (0,35ex){
\begin{tabular*}{\textwidth}{@{}l @{\extracolsep{\fill}} r@{}}
\begin{tabular}[t]{@{}l@{}}
Title\\Subtitle\\\begin{tiny}as of June 20, 2014\end{tiny}
\end{tabular} &
\begin{adjustbox}{valign=t}
\begin{pspicture}(0.7in,0.7in)
\psbarcode{http://www.ctan.org}{}{qrcode}
\end{pspicture}
\end{adjustbox}
\end{tabular*}
}[];


The end result of the MWE is a clipped forest with correctly placed Title and QR Code picture:

If the tabular* environment at the top of the document was moved to the commented line and varwidth option removed from the document class, it would look like below with Title and QRCode in the middle of the page, instead of at left and right side respectively:

Edit:

Tried to implement John's savebox tip from the comment, but the contents of savebox are rendered side by side with the tabular* environment, not above it. They are equal width though, which is good.

\documentclass[border=10pt]{standalone}
\begin{document}

\newsavebox{\forestbox}
\sbox{\forestbox}{
oooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooooo
}

\newlength{\forestboxlen}
\settowidth{\forestboxlen}{\usebox{\forestbox}}

\begin{tabular*}{\forestboxlen}{@{}l @{\extracolsep{\fill}} r@{}}
\begin{tabular}[t]{@{}l@{}}
Title\\ Text
\end{tabular} &
\begin{adjustbox}{valign=t}
\begin{pspicture}(0.7in,0.7in)
\psbarcode{http://www.ctan.org}{}{qrcode}
\end{pspicture}
\end{adjustbox}
\end{tabular*}

\usebox{\forestbox}

\end{document}

• When I compile your code, all of the nodes' contents is typeset in the first node. That is, Child3, for example, is typeset over Parent and Chaild31 and so on. – cfr Jun 20 '14 at 22:43
• @cfr Strange... I just copy/pasted MWE into the editor and compiled to produce the screenshots just added and everything rendered fine. Not sure what is happening here, it works for me. – ajeh Jun 23 '14 at 20:01
• Does \textwidth mean anything in standalone class? What you need to do is first determine the size of your tikzpicture (use a \savebox). – John Kormylo Jun 24 '14 at 3:07
• @JohnKormylo Two problems with that: 1. I can't think of a way to use savebox width before savebox itself was rendered. 2. When I tried to put Forest into savebox it no longer rendered. Actually, nothing in the savebox is rendering, so I am doing something wrong... – ajeh Jun 24 '14 at 13:01
• @JohnKormylo Figured out I was missing \usebox{\forestbox}! Thanks for the tip, but is it possible to put savebox contents under the title/picture, not side by side? – ajeh Jun 24 '14 at 13:40

## 2 Answers

I couldn't get the pst stuff to work, so used an image instead. I used \hfill to align things to the left and right sides. You need a minipage to define \textwidth.

\documentclass[border=10pt]{standalone}
\usepackage{forest}
\usepackage{adjustbox}
%\usepackage{pst-barcode}
%\usepackage[crop=off]{auto-pst-pdf}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}

\usepackage{graphicx}
\usepackage{mwe}

\newlength{\offset}
\newlength{\boxwidth}
\newsavebox{\mybox}

\setlength{\parindent=0pt}

\begin{document}

\savebox{\mybox}{% need to compute width
\begin{forest}
for tree={
draw=black, align=center, l sep=4ex, parent anchor=south, child anchor=north,
node options={font=\footnotesize, minimum width=14em, minimum height=10ex, rounded corners=1ex},
edge path={
\noexpand\path[\forestoption{edge}]
(!u.parent anchor) -- +(0,-2ex) -| (.child anchor)\forestoption{edge label};
},
where n children=0{
child anchor=west,
edge path={
\noexpand\path[\forestoption{edge}]
([xshift=-6em]!u.parent anchor) |- (.child anchor)\forestoption{edge label};
},
draw=red,for parent={
parent anchor=south, l sep=-12em, grow'=east, calign=child edge, draw=blue
}
}{}
}
[Parent,name=Parent
[Child1
[,phantom]
[Child11]
[Child12]
]
[Child3
[,phantom]
[Child31]
[Child32]
]
[Child3
[,phantom]
[Child31]
[Child32]
]
[Child3
[,phantom]
[Child31]
[Child32]
]
[Child3
[,phantom]
[Child31]
[Child32]
]
[Child3
[,phantom]
[Child31]
[Child32]
]
[Child3
[,phantom]
[Child31]
[Child32]
]
[Child3
[,phantom]
[Child31]
[Child32]
]
]
% <===== Insert below snippet with \node here =====
\end{forest}
}
\settowidth{\boxwidth}{\usebox{\mybox}}

\begin{minipage}{\boxwidth}
\setlength{\offset}{0.6\baselineskip}% distance from top to baseline for parbox
\addtolength{\offset}{-0.7in}% daistance from top to baseline for image

\parbox[t]{0.5\textwidth}{Title\\ Text}
\hfill\raisebox{\offset}{\includegraphics[width=0.7in,height=0.7in]{image}}
%\begin{pspicture}(0.7in,0.7in)
%    \psbarcode{http://www.ctan.org}{}{qrcode}
%\end{pspicture}

\usebox{\mybox}

\begin{tiny}Copyright \textcopyright Myself, 2014\end{tiny}
\end{minipage}
\end{document}


• Sorry, but you seem to be hardcoding sizes. This is for a machine-generated forest, which can be any arbitrary size, unknown until it is rendered, and thus has to work with standalone class. Am I missing anything? – ajeh Jun 24 '14 at 14:02
• I thought you were using standalone just to get a very large page. Ultimately you are going to have to fit this into a fixed size, which means using \includegraphics or \adjustbox to change the scale. The question is the relative sizes of the title etc. vs the forest. – John Kormylo Jun 24 '14 at 14:13
• If I could fit Forest inside a table cell, it would be trivial, but it is not rendering then. – ajeh Jun 24 '14 at 14:17
• Aha! To use standalone you need a minipage! – John Kormylo Jun 24 '14 at 14:39

Seems like including everything into a tabular environment solves all problems at once! The final version will need a logo included in the middle, and copyright at the bottom, but in general, this should work:

\documentclass[border=10pt]{standalone}
%\documentclass[border=10pt]{standalone}
\usepackage{forest}
\usepackage{adjustbox}
\usepackage{pst-barcode}
\usepackage[crop=off]{auto-pst-pdf}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}

\begin{document}

\begin{tabular}{l r}
\begin{tabular}[t]{@{}l@{}}
Title\\
Text\\
\end{tabular} &
\begin{adjustbox}{valign=t}
\begin{pspicture}(0.7in,0.7in)
\psbarcode{http://www.ctan.org}{}{qrcode}
\end{pspicture}
\end{adjustbox} \\
\multicolumn{2}{c}{
\begin{forest}
for tree={
draw=black, align=center, l sep=4ex, parent anchor=south, child anchor=north,
node options={font=\footnotesize, minimum width=14em, minimum height=10ex, rounded corners=1ex},
edge path={
\noexpand\path[\forestoption{edge}]
(!u.parent anchor) -- +(0,-2ex) -| (.child anchor)\forestoption{edge label};
},
where n children=0{
child anchor=west,
edge path={
\noexpand\path[\forestoption{edge}]
([xshift=-6em]!u.parent anchor) |- (.child anchor)\forestoption{edge label};
},
draw=red,for parent={
parent anchor=south, l sep=-12em, grow'=east, calign=child edge, draw=blue
}
}{}
}
[Parent,name=Parent
[Child1
[,phantom]
[Child11]
[Child12]
]
[Child3
[,phantom]
[Child31]
[Child32]
]
[Child4
[,phantom]
[Child41]
[Child42]
]
]
\end{forest}
}
\end{tabular}
\end{document}


• When you think about how tabular works, both solutions are doing the same thing. I will admit the tabular is prettier. You don'r really need to put a tabular inside a tabular, and you could make it faster using a savebox. – John Kormylo Jun 24 '14 at 18:29
• Tabular inside a tabular serves two purposes: 1) to allow multi-line Title and 2) to vertically align the tops of Title and QR code as I understand. But I understand very little :) – ajeh Jun 24 '14 at 18:41