Cómo acceder a la longitud de la lista interna en pgffor

Cómo acceder a la longitud de la lista interna en pgffor

Tengo una lista de listas y estoy usando \foreachbucles anidados para recorrerlas en iteración. Al usar [count=\var], puedo usarlo \varpara acceder a la longitud del bucle externo (después de haberlo iterado). Sin embargo, no puedo utilizar este método para acceder a la longitud de los bucles internos. En mi caso, todos los bucles internos deberían tener la misma longitud, pero técnicamente quería acceder a la longitud del último de los bucles internos. Esto es lo que tengo hasta ahora:

\documentclass{article}
\usepackage{tikz}
\begin{document}

% The first two work:

\begin{tikzpicture}
  \foreach \from [count=\to] in {2,3,1} {
    \draw (\from,1) -- (\to,2);
  }
  \draw[gray] (0.5,0.5) rectangle (\to+0.5,2.5);
\end{tikzpicture}

\begin{tikzpicture}
  \foreach \from [count=\to] in {1,3,2} {
    \draw (\from,1) -- (\to,2);
  }
  \draw[gray] (0.5,0.5) rectangle (\to+0.5,2.5);
\end{tikzpicture}

% This one does not work:

\begin{tikzpicture}
  \foreach \list [count=\row] in {{2,3,1},{1,3,2}} {
    \foreach \from [count=\to] in \list {
      \draw (\from,\row) -- (\to,\row+1);
    }
  }
  \draw[gray] (0.5,0.5) rectangle (\to+0.5,\row+1.5);
\end{tikzpicture}

\end{document}

Esto es con lo que quiero terminar:

resultado deseado

Respuesta1

Utiliza contadores LaTeX en combinación con macros TikZ. Todas las contraoperaciones son globales.

\documentclass{article}
\usepackage{tikz}

\newcounter{to}
\newcounter{row}

\begin{document}


\begin{tikzpicture}
  \setcounter{to}{0}
  \foreach \from in {2,3,1} {
    \stepcounter{to}
    \draw (\from,1) -- ({\theto},2);
  }
  \draw[gray] (0.5,0.5) rectangle (\theto+0.5,2.5);
\end{tikzpicture}

\begin{tikzpicture}
  \setcounter{to}{0}
  \foreach \from in {1,3,2} {
    \stepcounter{to}
    \draw (\from,1) -- (\theto,2);
  }
  \draw[gray] (0.5,0.5) rectangle (\theto+0.5,2.5);
\end{tikzpicture}


\begin{tikzpicture}
  \setcounter{row}{0}
  \foreach \list in {{2,3,1},{1,3,2}} {
    \stepcounter{row}
    \setcounter{to}{0}
    \foreach \from in \list {
      \stepcounter{to}
      \draw (\from,\therow) -- (\theto,\therow+1);
    }
  }
  \draw[gray] (0.5,0.5) rectangle (\theto+0.5,\therow+1.5);
\end{tikzpicture}

\end{document}

manifestación

Respuesta2

Esta solución tiene la desventaja de necesitar recorrer la lista varias veces, pero evita establecer una variable global.

\documentclass{article}
\usepackage{tikz}
\begin{document}

\begin{tikzpicture}
  \foreach \list [count=\row] in {{2,3,1},{1,3,2}} {
    \foreach \from [count=\to] in \list {
      \draw (\from,\row) -- (\to,\row+1);
    }
  }
  \foreach \list [count=\count] in {{2,3,1},{1,3,2}} {
    \ifx \count \row
      \foreach \from [count=\to] in \list {
      }
      \draw[gray] (0.5,0.5) rectangle (\to+0.5,\row+1.5);
    \fi
  }
\end{tikzpicture}

\end{document}

El problema con mi primera versión es que \toestaba fuera de alcance cuando intenté usarla. Esto lo usa cuando todavía está dentro del alcance.

información relacionada