3

Starting from this example Boxes with text and math , I wanted to stack different boxes to have the first 2 above the others. But I got the following result: enter image description here My code is the following:

\documentclass[border = 0.5cm]{standalone}
\usepackage{tikz}
  \usetikzlibrary{calc, arrows, shapes, decorations.pathmorphing}
\usepackage{mychemistry}
\usepackage[version=4]{mhchem}

% molecules definition
\newcommand{\PsixG}[1]{\chemfig[line width=1.5pt][scale=#1]{HO-[:-30]-[:30]-[:-30]O-[:30]-[:-30]-[:30]O-[:-30]-[:30]-[:-30]O-[:30]-[:-30]-[:30]O-[:-30]-[:30]-[:-30]O-[:30]-[:-30]-[:30]OH}}
\newcommand{\unPE}[1]{\chemfig[line width=1.5pt][scale=#1]{HO-[:-30]-[:30]-[:-30]O-[:30]-[:-30]-[:30]O-[:-30]-[:30]-[:-30]O-[:30]-[:-30]-[:30]O-[:-30]-[:30]-[:-30]OH}}
\newcommand{\PsixGald}[1]{\chemfig[line width=1.5pt][scale=#1]{HO-[:-30]-[:30]-[:-30]O-[:30]-[:-30]-[:30]O-[:-30]-[:30]-[:-30]O-[:30]-[:-30]-[:30]O-[:-30]-[:30]-[:-30]O-[:30]-[:-30]=[:30]O}}
\newcommand{\unPEald}[1]{\chemfig[line width=2pt][scale=#1]{HO-[:-30]-[:30]-[:-30]O-[:30]-[:-30]-[:30]O-[:-30]-[:30]-[:-30]O-[:30]-[:-30]-[:30]O-[:-30]-[:30]=[:-30]O}}
\newcommand{\PsixGacid}[1]{\chemfig[line width=1.5pt][scale=#1]{HO-[:-30]-[:30]-[:-30]O-[:30]-[:-30]-[:30]O-[:-30]-[:30]-[:-30]O-[:30]-[:-30]-[:30]O-[:-30]-[:30]-[:-30]O-[:30]-[:-30](-[:-90]OH)=[:30]O}}
\newcommand{\oxalic}[1]{\chemfig[line width=1.5pt][scale=#1]{HO-[:-30](=[:-90]O)-[:30](=[:90]O)-[:-30]OH}}

\begin{document}
% Define a few styles and constants
\tikzstyle{glycols} = [draw=red,fill=red!10, very thick,
rectangle, rounded corners, inner sep=10pt]
\tikzstyle{glyctitle} =[fill=red, text=white]
\tikzstyle{alds} = [draw=green,fill=green!10, very thick,
rectangle, rounded corners, inner sep=10pt]
\tikzstyle{aldstitle} =[fill=green, text=white]
\tikzstyle{acids} = [draw=blue,fill=blue!10, very thick,
rectangle, rounded corners, inner sep=10pt]
\tikzstyle{acidtitle} =[fill=blue, text=white]
\tikzstyle{IC} = [draw=purple,fill=purple!10, very thick,
rectangle, rounded corners, inner sep=10pt]
\tikzstyle{ICtitle} =[fill=purple, text=white]

\begin{tikzpicture}
\node [glycols] (Gn){%
    \begin{minipage}{0.50\textwidth}
\PsixG{0.35}

\unPE{0.35}
    \end{minipage}
};    
\node[glyctitle, right=10pt] at (Gn.north west) {Glycols};
\end{tikzpicture}

\begin{tikzpicture}
\node [alds] (Ald){%
    \begin{minipage}{0.50\textwidth}
\PsixGald{0.35}

\unPEald{0.35}
    \end{minipage}
}; 
\node[aldstitle, right=10pt] at (Ald.north west) {Aldehydes};    
\end{tikzpicture}

\begin{tikzpicture}
\node [acids] (Acid){%
    \begin{minipage}{0.50\textwidth}
\PsixGacid{0.35}
    \end{minipage}
}; 
\node[acidtitle, right=10pt] at (Acid.north west) {Acids};    
\end{tikzpicture}

\begin{tikzpicture}
\node [IC] (IC){%
    \begin{minipage}{0.50\textwidth}
\oxalic{0.35}
    \end{minipage}
}; 
\node[ICtitle, right=10pt] at (IC.north west) {IC};    
\end{tikzpicture}

\end{document}

My question is: is it possible to adapt this code to get the stacked boxes, or should I proceed differently and put all the nodes in a single tikzpicture ? The boxes may vary (in my real picture) in height, by adding other molecules and in width (I can adjust this to the longest molecule with the chemfig scale factor). Any suggestion ?

2

The problem is that standalone will adapt the dimensions of the paper to accommodate the content. As a consequence linebreaking will not hapen automatically. To enforce linebreaking, you could surround your content with a minipage, then an empty line between your boxes will result in a new line.

\documentclass[border = 0.5cm]{standalone}
\usepackage{tikz}
  \usetikzlibrary{calc, arrows, shapes, decorations.pathmorphing}
\usepackage{mychemistry}
\usepackage[version=4]{mhchem}

% molecules definition
\newcommand{\PsixG}[1]{\chemfig[line width=1.5pt][scale=#1]{HO-[:-30]-[:30]-[:-30]O-[:30]-[:-30]-[:30]O-[:-30]-[:30]-[:-30]O-[:30]-[:-30]-[:30]O-[:-30]-[:30]-[:-30]O-[:30]-[:-30]-[:30]OH}}
\newcommand{\unPE}[1]{\chemfig[line width=1.5pt][scale=#1]{HO-[:-30]-[:30]-[:-30]O-[:30]-[:-30]-[:30]O-[:-30]-[:30]-[:-30]O-[:30]-[:-30]-[:30]O-[:-30]-[:30]-[:-30]OH}}
\newcommand{\PsixGald}[1]{\chemfig[line width=1.5pt][scale=#1]{HO-[:-30]-[:30]-[:-30]O-[:30]-[:-30]-[:30]O-[:-30]-[:30]-[:-30]O-[:30]-[:-30]-[:30]O-[:-30]-[:30]-[:-30]O-[:30]-[:-30]=[:30]O}}
\newcommand{\unPEald}[1]{\chemfig[line width=2pt][scale=#1]{HO-[:-30]-[:30]-[:-30]O-[:30]-[:-30]-[:30]O-[:-30]-[:30]-[:-30]O-[:30]-[:-30]-[:30]O-[:-30]-[:30]=[:-30]O}}
\newcommand{\PsixGacid}[1]{\chemfig[line width=1.5pt][scale=#1]{HO-[:-30]-[:30]-[:-30]O-[:30]-[:-30]-[:30]O-[:-30]-[:30]-[:-30]O-[:30]-[:-30]-[:30]O-[:-30]-[:30]-[:-30]O-[:30]-[:-30](-[:-90]OH)=[:30]O}}
\newcommand{\oxalic}[1]{\chemfig[line width=1.5pt][scale=#1]{HO-[:-30](=[:-90]O)-[:30](=[:90]O)-[:-30]OH}}

\begin{document}
% Define a few styles and constants
\tikzstyle{glycols} = [draw=red,fill=red!10, very thick,
rectangle, rounded corners, inner sep=10pt]
\tikzstyle{glyctitle} =[fill=red, text=white]
\tikzstyle{alds} = [draw=green,fill=green!10, very thick,
rectangle, rounded corners, inner sep=10pt]
\tikzstyle{aldstitle} =[fill=green, text=white]
\tikzstyle{acids} = [draw=blue,fill=blue!10, very thick,
rectangle, rounded corners, inner sep=10pt]
\tikzstyle{acidtitle} =[fill=blue, text=white]
\tikzstyle{IC} = [draw=purple,fill=purple!10, very thick,
rectangle, rounded corners, inner sep=10pt]
\tikzstyle{ICtitle} =[fill=purple, text=white]

\begin{minipage}{\textwidth}
\hskip-.1\textwidth
\begin{tikzpicture}
\node [glycols] (Gn){%
    \begin{minipage}{0.50\textwidth}
\PsixG{0.35}

\unPE{0.35}
    \end{minipage}
};    
\node[glyctitle, right=10pt] at (Gn.north west) {Glycols};
\end{tikzpicture}
\begin{tikzpicture}
\node [alds] (Ald){%
    \begin{minipage}{0.50\textwidth}
\PsixGald{0.35}

\unPEald{0.35}
    \end{minipage}
}; 
\node[aldstitle, right=10pt] at (Ald.north west) {Aldehydes};    
\end{tikzpicture}

\hskip-.1\textwidth
\begin{tikzpicture}
\node [acids] (Acid){%
    \begin{minipage}{0.50\textwidth}
\PsixGacid{0.35}
    \end{minipage}
}; 
\node[acidtitle, right=10pt] at (Acid.north west) {Acids};    
\end{tikzpicture}
\begin{tikzpicture}
\node [IC] (IC){%
    \begin{minipage}{0.50\textwidth}
\oxalic{0.35}
    \end{minipage}
}; 
\node[ICtitle, right=10pt] at (IC.north west) {IC};    
\end{tikzpicture}
\end{minipage}

\end{document}

enter image description here

  • That's brilliant, just adding an external minipage does the job! So the culprit was 'standalone'. Thanks a lot! – DRi Aug 30 '16 at 15:24
  • nice. The same effect can be obtained using 'tcolorbox', might be worth looking at, but nice! Just a comment on the white text on green background, very hard to read! Easy to fix though.... – Leeser Aug 30 '16 at 16:36
  • @Leeser You are right, I already changed the colors to have white text on dark background and black text on light background – DRi Aug 31 '16 at 15:07

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.