问题
我可以自动引用 TikZ 中节点的最低基线吗?
情况
我想在节点的最低基线下用红色画一条线(不只是moonfact.south west
-moonfact.south east
)。
示例代码
\documentclass{article}
\usepackage{fontspec}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[remember picture,baseline,every node/.style={draw,red,inner sep=0pt,outer sep=0pt}]%
\node [align=left,text width=5em] (moonfact) {The moon is 384,400 kilometers away from earth.};
\end{tikzpicture}%
\begin{tikzpicture}[overlay,remember picture,baseline]%
\draw ([yshift=-1ex]moonfact.base west) -- ([yshift=-1ex]moonfact.base east);
\end{tikzpicture}%
\end{document}
以 10px 边框独立输出
比较两个带有 和 的示例,anchor=north
使用south west
和south east
代替base west
和base east
。 g 影响行距。使用“末行基线”可以避免这种情况:
答案1
不确定这是否有帮助,但\strut
在每个节点文本后引入一个将会调整底部节点的限制。
\documentclass{article}
\usepackage{fontspec}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}[remember picture,baseline,every node/.style={draw,red,inner sep=0pt,outer sep=0pt}]%
\node [align=left,text width=5em] (moonfact) {The moon is 384,400 kilometers away from earth.\strut};
\node [align=left,text width=5em,anchor=north west,at=(moonfact.north east)] (car) {My car is forty meters away from my garage.\strut};
\end{tikzpicture}%
\begin{tikzpicture}[overlay,remember picture,baseline]%
\draw ([yshift=-1ex]moonfact.south west) -- ([yshift=-1ex]moonfact.south east);
\draw[blue] ([yshift=-1ex]car.south west) -- ([yshift=-1ex]car.south east);
\end{tikzpicture}%
\end{document}