从这个例子开始带有文本和数学的方框,我想堆叠不同的盒子,让前两个盒子位于其他盒子上方。但我得到了以下结果: 我的代码如下:
\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}
我的问题是:是否可以调整此代码以获取堆叠的框,或者我应该采取不同的方式并将所有节点放在单个 tikzpicture 中?框的高度可能会有所不同(在我的真实图片中),通过添加其他分子和宽度(我可以使用 chemfig 比例因子将其调整为最长的分子)。有什么建议吗?
答案1
问题是standalone
会调整纸张尺寸以适应内容。因此不会自动换行。要强制换行,您可以用 包围内容minipage
,然后方框之间的空行将产生新行。
\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
\tikzset{
glycols/.style={draw=red,fill=red!10, very thick,
rectangle, rounded corners, inner sep=10pt},
glyctitle/.style={fill=red, text=white},
alds/.style={draw=green,fill=green!10, very thick,
rectangle, rounded corners, inner sep=10pt},
aldstitle/.style={fill=green, text=white},
acids/.style={draw=blue,fill=blue!10, very thick,
rectangle, rounded corners, inner sep=10pt},
acidtitle/.style={fill=blue, text=white},
IC/.style={draw=purple,fill=purple!10, very thick,
rectangle, rounded corners, inner sep=10pt},
ICtitle/.style={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}