考虑下面的代码。
\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10,
ticks=none}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{backgrounds,
calc,
patterns}
\begin{document}
\begin{tikzpicture}
\tikzset{
tile/.pic={
\draw[line width=2mm, pic actions] (0, 0) -| (2, 1) -| (2, 3) -| (2, 3) -| cycle;
}
}
\path (0,0) pic {tile};
\end{tikzpicture}
\quad
\begin{tikzpicture}%[scale=4, transform shape]
\tikzset{
tile/.pic={
\draw[line width=2mm, pic actions] (0, 0) -| (2, 1) -| (1, 2) -| (2, 3) -| cycle;
}
}
\path (0,0) pic {tile} (0, 3) pic {tile};
\end{tikzpicture}
\end{document}
它输出以下内容:
我想要让两个图像中的第一个(即矩形)垂直居中。我该如何实现?
答案1
我把每一个都放在tikzpicture
各自的位置上$\vcenter{\hbox{...}}$
。
在standalone
文档中,这已经足够了。在常规文档中,知道\vcenter
内容将围绕数学轴居中,这意味着部分图像可能会延伸到基线以下。根据周围材料,可能需要额外的步骤来平衡与周围材料的垂直间距。
\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10,
ticks=none}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{backgrounds,
calc,
patterns}
\begin{document}
$\vcenter{\hbox{\begin{tikzpicture}
\tikzset{
tile/.pic={
\draw[line width=2mm, pic actions] (0, 0) -| (2, 1) -| (2, 3) -| (2, 3) -| cycle;
}
}
\path (0,0) pic {tile};
\end{tikzpicture}}}$%
\quad
$\vcenter{\hbox{\begin{tikzpicture}%[scale=4, transform shape]
\tikzset{
tile/.pic={
\draw[line width=2mm, pic actions] (0, 0) -| (2, 1) -| (1, 2) -| (2, 3) -| cycle;
}
}
\path (0,0) pic {tile} (0, 3) pic {tile};
\end{tikzpicture}}}$
\end{document}
这是一种让所有内容保持在基线之上、使用\raisebox
和测量内容高度的方法:
\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10,
ticks=none}
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{backgrounds,
calc,
patterns}
\begin{document}
\setbox0=\hbox{\begin{tikzpicture}
\tikzset{
tile/.pic={
\draw[line width=2mm, pic actions] (0, 0) -| (2, 1) -| (2, 3) -| (2, 3) -| cycle;
}
}
\path (0,0) pic {tile};
\end{tikzpicture}}%
\setbox2=\hbox{\begin{tikzpicture}%[scale=4, transform shape]
\tikzset{
tile/.pic={
\draw[line width=2mm, pic actions] (0, 0) -| (2, 1) -| (1, 2) -| (2, 3) -| cycle;
}
}
\path (0,0) pic {tile} (0, 3) pic {tile};
\end{tikzpicture}}%
\raisebox{.5\dimexpr\ht2-\ht0\relax}{\copy0}\quad\box2
\end{document}
答案2
我不一定会使用单独的tikzpicture
s。
\documentclass[tikz,border=5mm]{standalone}
\begin{document}
\begin{tikzpicture}
\begin{scope}[tile/.pic={
\draw[line width=2mm, pic actions] (0, 0) -| (2, 1) -| (2, 3) -| (2, 3) -| cycle;
}
]
\path (0,1.5) pic {tile};
\end{scope}
\begin{scope}[xshift=pi*1cm,tile/.pic={
\draw[line width=2mm, pic actions] (0, 0) -| (2, 1) -| (1, 2) -| (2, 3) -| cycle;
}]
\path (0,0) pic {tile} (0, 3) pic {tile};
\end{scope}
\end{tikzpicture}
\end{document}