我有三个大小相同的节点,但包含不同长度的文本。这导致其中两个节点出现换行。
我希望所有文本与第三个节点中的最后一行文本对齐。我调整了文本深度,但由于 LaTeX 将第三个节点中的文本读取为一行,因此我认为它没有按照我想要的方式工作。
这个问题有简单的解决办法吗?任何指导都值得感激!
\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
\node [draw, text width=5em, minimum height = 5em, align = center] (node1) {One line};
\node [draw, right = 1cm of node1, text width=5em, minimum height = 5em, align = center] (node2) {One linebreak};
\node [draw, right = 1cm of node2, text width=5em, minimum height = 5em, align = center] (node3) {This text has two linebreaks};
\end{tikzpicture}
\end{document}
答案1
您可以将文本与框分开放置:
\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
\node [draw, text width=5em, minimum height = 5em] (node1) {};
\node [draw, right = 1cm of node1, text width=5em, minimum height = 5em] (node2) {};
\node [draw, right = 1cm of node2, text width=5em, minimum height = 5em] (node3) {};
\node[text width=5em, align=center] (node3text) at (node3.center) {This text has two linebreaks};
\node[anchor=south, text width=5em, align=center] at (node3text.south -| node2.center) {One linebreak};
\node[anchor=south, text width=5em, align=center] at (node3text.south -| node1.center) {One line};
\end{tikzpicture}
\end{document}
答案2
这是临时解决方案。如果您可以估算出最高文本的高度,则可以使用适当的\vbox
:
\documentclass{minimal}
\usepackage{tikz}
\usetikzlibrary{positioning}
\begin{document}
\begin{tikzpicture}
\node [draw, text width=5em, minimum height = 5em, align = center] (node1) {\vbox to 3em{\vfill One line}};
\node [draw, right = 1cm of node1, text width=5em, minimum height = 5em, align = center] (node2) {\vbox to 3em{\vfill One linebreak}};
\node [draw, right = 1cm of node2, text width=5em, minimum height = 5em, align = center] (node3) {\vbox to 3em{\vfill This text has two linebreaks}};
\end{tikzpicture}
\end{document}
答案3
您还可以使用equal height group
并tcolorboxes
定义bottom
垂直对齐。
\documentclass{article}
\usepackage{tcolorbox}
\tcbset{colback=white, sharp corners, width=2.5cm, equal height group=AT, before=, after=\hfill, notitle, halign=center, valign=bottom}
\begin{document}
\begin{tcolorbox}
One line
\end{tcolorbox}
\begin{tcolorbox}
One\\ linebreak
\end{tcolorbox}
\begin{tcolorbox}
This text\\ has two\\ linebreaks\end{tcolorbox}
\end{document}
经过两次编译后结果是: