I would like to be able to write something like the following:
\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}
But, for some reason, no lengths are actually calculated. So I tried to find a pgf solution and came up with the following:
\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}
But this isn't really calculating the width of the text I want to work with. What I would like to do is write something like
\pgfmathparse{width("\texttt{\myn}")}
But that results in an error:
! Argument of \XC@definec@lor has an extra }.
<inserted text>
\par
l.28 }
?
I've also tried:
\pgfmathparse{width("\noexpand\texttt{\myn}")}
without any luck either.
How can I make a calculation of the true textwidth that's being formatted within the \foreach loop?
UPDATE
There's something about the tikzpicture environment that seems to dislike or prevent my code for measuring the width of text from working. For example, everything compiles fine in the example below (sans 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}
So, this lead me to consider writing the following:
\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}
which works! But, the tikz manual specifically says that \pgfinterruptpicture is not meant to be used in this fashion.
Apart from getting a valid working example, I would also like to understand why my original example above fails to work as expected.
\pgfextrato be able to compare the lengths I was working with. But, this seems to be contrary to what the manual says about the use of\pgfextrabeing restricted to inside a path. – A.Ellett Nov 09 '14 at 01:49[font=\ttfamily] \pgfmathsetmacro{\mywidth}{width("\myn")};do what you want? – cfr Nov 09 '14 at 03:44\settowidthcode of the first example fails and why I seem to need the\pgfextrawhen I'm not in a path construction. – A.Ellett Nov 09 '14 at 04:20\pgfextraif you use\pgfmathsetmacro... – cfr Nov 09 '14 at 13:25