2

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.

A.Ellett
  • 50,533
  • I just updated the question to reflect that I'm not necessarily able to calculate the width of the formatted text from the width of the node. – A.Ellett Nov 09 '14 at 01:44
  • Also, I had to resort to using \pgfextra to 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 \pgfextra being restricted to inside a path. – A.Ellett Nov 09 '14 at 01:49
  • Does [font=\ttfamily] \pgfmathsetmacro{\mywidth}{width("\myn")}; do what you want? – cfr Nov 09 '14 at 03:44
  • @cfr It might. But I'd be much more interested in understanding why the \settowidth code of the first example fails and why I seem to need the \pgfextra when I'm not in a path construction. – A.Ellett Nov 09 '14 at 04:20
  • Hmmm... Yes. Of course, you don't need \pgfextra if you use \pgfmathsetmacro... – cfr Nov 09 '14 at 13:25

1 Answers1

2

This is what I wound up going with:

\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}

Explanations for why this code works can be found at \settowidth problem in tikz and in groups and at How can I use an hbox inside a TikZ environment for text dimension measurement?

A.Ellett
  • 50,533