I wish to use the code below in order to get the maximum length of a list of words.
\documentclass[border=5mm]{standalone}
\usepackage{tikz}
\usepackage{calc}
\begin{document}
\begin{tikzpicture}
\def\words{%
xxx,
xxxxxx,
xxxxxxxxx%
}
\newlength{\mywidth}
\setlength\mywidth{0pt}
\foreach \word in \words {%
\setlength\mywidth{\maxof{\mywidth}{\widthof{word}}}
\message{showinfo1 \word -> \the\mywidth};
}
\message{showinfo2 \the\mywidth};
\end{tikzpicture}
\end{document}
But output results looks like weird:
showinfo1 xxx-> 21.4167pt
showinfo1 xxxxxx-> 21.4167pt
showinfo1 xxxxxxxxx-> 21.4167pt
showinfo2 0.0pt
- The showinfo1 should be increase one by one since word length increase, but actual output the same value
- The showinfo2 should be the last valid value but it show as 0.
Any global version should I use?
wordas an argument. That's always the same length. You probably wanted\word. A PGFFor scopes its body but the\setlengthassignment is local and will be gone after the loop. You will need to use some form of loop that doesn't scope its body, some form of\gsetlengthor a different approach. (Since you're already using TikZ you could use\pgfmathsetlength\mywidth{max(width("xxx"),width("xxxxxx"),…))}or\pgfkeys{max length/.code=\setlength\mywidth{\maxof{\mywidth}{\widthof{#1}}}, max length/.list/.expand once=\words}. – Qrrbrbirlbel Nov 26 '23 at 03:10