我希望能够写出类似下面的内容:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\newlength\aelengtha
\newlength\aelengthb
\newlength\aelengthc
\begin{document}
\begin{tikzpicture}
\coordinate (Q) at (0,0);
\foreach \myn [count=\myc from=1]
in {apple,
orange,
this is a long line,
short}
{
\pgfextra{%%
\settowidth\aelengthb{\texttt{\myn}}
\ifdim\aelengtha<\aelengthb
\global\aelengtha=\aelengthb
\typeout{==>resetting=\the\aelengtha}
\fi}
\typeout{==>\the\aelengthb}
\node[anchor=base west,font=\ttfamily] at ($(Q)+(0,-\myc\baselineskip)$) {this \myn\ is an item};
}
\end{tikzpicture}
\end{document}
但是,出于某种原因,实际上没有计算长度。因此,我尝试找到pgf
解决方案,并得出以下结论:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\newlength\aelengtha
\newlength\aelengthb
\newlength\aelengthc
\begin{document}
\begin{tikzpicture}
\coordinate (Q) at (0,0);
\foreach \myn [count=\myc from=1]
in {apple,
orange,
this is a long line,
short}
{
\pgfmathparse{width("\myn")}
\aelengthb=\pgfmathresult pt
\pgfextra{%%
\ifdim\aelengtha<\aelengthb
\global\aelengtha=\aelengthb
\typeout{==>resetting=\the\aelengtha}
\fi}
\typeout{==>\the\aelengthb}
\node[anchor=base west,font=\ttfamily] at ($(Q)+(0,-\myc\baselineskip)$) {this is \myn\ an item};
}
\end{tikzpicture}
\typeout{==>\the\aelengtha}
\end{document}
但这实际上并不是计算我想要处理的文本的宽度。我想做的是写类似
\pgfmathparse{width("\texttt{\myn}")}
但这会导致错误:
! Argument of \XC@definec@lor has an extra }.
<inserted text>
\par
l.28 }
?
我也尝试过:
\pgfmathparse{width("\noexpand\texttt{\myn}")}
也没有任何运气。
我如何计算真的在循环内格式化的文本宽度\foreach
?
更新
环境似乎有些问题tikzpicture
,让我的代码无法正常工作,或者无法测量文本的宽度。例如,下面的示例中的所有内容都可以正常编译(无tikzpicture
)
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\newlength\aelengtha
\newlength\aelengthb
\newlength\aelengthc
\begin{document}
\foreach \myn [count=\myc from=1]
in {apple,
orange,
this is a long line,
short}
{
\settowidth\aelengthb{\texttt{\myn}}%%
\ifdim\aelengtha<\aelengthb
\global\aelengtha=\aelengthb
\typeout{==>Length A=\the\aelengtha : recalculated}
\fi
\typeout{==>length B=\the\aelengthb}
}
\typeout{==>length A=\the\aelengtha : Final}
\the\aelengtha
\end{document}
因此,这让我考虑写以下内容:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\newlength\aelengtha
\newlength\aelengthb
\newlength\aelengthc
\begin{document}
\begin{tikzpicture}
\coordinate (Q) at (0,0);
\foreach \myn [count=\myc from=1]
in {apple,
orange,
this is a long line,
short}
{
\pgfinterruptpicture
\settowidth\aelengthb{\texttt{\myn}}%%
\ifdim\aelengtha<\aelengthb
\global\aelengtha=\aelengthb
\typeout{==>Length A=\the\aelengtha : Resetting}%%
\fi
\typeout{==>Length B=\the\aelengthb}%%
\endpgfinterruptpicture
\node[anchor=base west,font=\ttfamily] at ($(Q)+(0,-\myc\baselineskip)$) {\myn};
}
\end{tikzpicture}
\end{document}
有效!但是,tikz
手册明确指出,\pgfinterruptpicture
不应以这种方式使用。
除了获得有效的工作示例,我会还想了解为什么我上面的原始示例无法按预期工作。
答案1
这就是我最终的选择:
\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{calc}
\newlength\aelengtha
\newlength\aelengthb
\newlength\aelengthc
\begin{document}
\begin{tikzpicture}
\coordinate (Q) at (0,0);
\foreach \myn [count=\myc from=1]
in {apple,
orange,
this is a long line,
short}
{
\settowidth\aelengthb{\pgfinterruptpicture\texttt{\myn}\endpgfinterruptpicture}%%
\ifdim\aelengtha<\aelengthb
\global\aelengtha=\aelengthb
\typeout{==>Length A=\the\aelengtha : Resetting}%%
\fi
\typeout{==>Length B=\the\aelengthb}%%
\node[anchor=base west,font=\ttfamily] at ($(Q)+(0,-\myc\baselineskip)$) {\myn};
}
\end{tikzpicture}
\end{document}
关于此代码为何有效的解释,请参见tikz 和组中的 \settowidth 问题以及如何在 TikZ 环境中使用 hbox 进行文本尺寸测量?