我想更改节点中文本的字体大小。理想情况下,最终我会在节点中拥有多种字体大小。但是,当我尝试更改节点中的字体大小时,我得到了意想不到的不同行距。以下是代码:
\documentclass[tikz]{standalone}
\begin{document}
\begin{tikzpicture}
\node[anchor=north east,text width=0.2\textwidth,font=\small] at (0,0) {This text demonstrates how your own text will look when you replace the placeholder with your own text.};
\node[anchor=north west,text width=0.2\textwidth] at (0,0) {\small This text demonstrates how your own text will look when you replace the placeholder with your own text.};
\end{tikzpicture}
\end{document}
如您所见,在节点中设置字体大小会导致不同的行距。我该如何避免这种情况?那里发生了什么?
答案1
很可能,节点的文本被封闭在一个组中,并且\par
触发换行的结尾在这个组之外,因此标准基线距离有效,而不是与 相关的基线距离\small
。
您可以在下面看到相同的效果:
\documentclass{article}
\begin{document}
\begin{minipage}[t]{0.2\textwidth}
\raggedright
\small
This text demonstrates how your own text will
look when you replace the placeholder with
your own text.
\end{minipage}
\quad
\begin{minipage}[t]{0.2\textwidth}
\raggedright
{\small
This text demonstrates how your own text will
look when you replace the placeholder with
your own text.}
\end{minipage}
\end{document}
添加\par
。
\documentclass{standalone}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\node[anchor=north east,text width=0.2\textwidth,font=\small] at (0,0) {This text demonstrates
how your own text will look when you replace the placeholder with your own text.};
\node[anchor=north west,text width=0.2\textwidth] at (0,0) {\small This text demonstrates how
your own text will look when you replace the placeholder with your own text.\par};
\end{tikzpicture}
\end{document}
答案2
@egreg 不是答案,而是观察。如果重叠这两个节点,您会发现单词之间的空间看起来有些不同,但这两个节点的尺寸似乎完全相同。
\documentclass{article}
\usepackage{tikz}
\begin{document}
\begin{tikzpicture}
\node[anchor=north east,text width=0.2\textwidth,font=\small,draw] at (0,0) {This text demonstrates how your own text will look when you replace the placeholder with your own text.};
\node[anchor=north east,text width=0.2\textwidth,red,opacity=0.5,draw] at (0,0) {\small This text demonstrates how your own text will look when you replace the placeholder with your own text.\par};
\end{tikzpicture}
\end{document}