Graphics, Figures & TablesUsing Counter Value as Argument

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
hnc3313
Posts: 1
Joined: Tue Aug 13, 2024 8:45 pm

Using Counter Value as Argument

Post by hnc3313 »

I'm having trouble finding a way past this issue. I'm trying to use the library tkz-graph, which pulls on tikz, to quickly generate graphs.

What I'd like to do is iterate through a loop and generate a graph on each pass. I think I have set my loop and counter up to work correctly, but I seem to be unable to use the value of the counter as an argument for making my graph.

For example, this works:

Code: Select all

\documentclass{article}
\usepackage{graphicx} % Required for inserting images
\usepackage{forloop}
\usepackage{tkz-graph}
\usetikzlibrary{shapes.geometric}


\begin{document}


\newcounter  {a}


\forloop{a}{0}{\value{a} < 2}
{
                    \begin{figure}
                    \centering

                    \begin{tikzpicture}

                        \Vertices[Lpos=90, unit=5]{circle}{0,1,2,3}
                        \Edges[color=black, lw=2pt](0,2)
 %                       \Edges[color=black, lw=2pt](\value{a},1)

                    \end{tikzpicture}

                    \end{figure}

}

\end{document}

But uncommenting the second "Edge" line does not. It gives a "Missing \endcsname inserted" error.

Code: Select all

\documentclass{article}
\usepackage{graphicx} % Required for inserting images
\usepackage{forloop}
\usepackage{tkz-graph}
\usetikzlibrary{shapes.geometric}


\begin{document}


\newcounter  {a}


\forloop{a}{0}{\value{a} < 2}
{
                    \begin{figure}
                    \centering

                    \begin{tikzpicture}

                        \Vertices[Lpos=90, unit=5]{circle}{0,1,2,3}
                        \Edges[color=black, lw=2pt](0,2)
                        \Edges[color=black, lw=2pt](\value{a},1)

                    \end{tikzpicture}

                    \end{figure}

}

\end{document}


I have also tried to use \numexpr to force the argument to be evaluated as a number, but that also results in an \endcsname error.

Code: Select all

\documentclass{article}
\usepackage{graphicx} % Required for inserting images
\usepackage{forloop}
\usepackage{tkz-graph}
\usetikzlibrary{shapes.geometric}


\begin{document}


\newcounter  {a}


\forloop{a}{0}{\value{a} < 2}
{
                    \begin{figure}
                    \centering

                    \begin{tikzpicture}

                        \Vertices[Lpos=90, unit=5]{circle}{0,1,2,3}
                        \Edges[color=black, lw=2pt](0,2)
                        \Edges[color=black, lw=2pt](\numexpr{\value{a}},1)

                    \end{tikzpicture}

                    \end{figure}

}

\end{document}


If anyone has any thoughts on how to accomplish this, I'd appreciate it. The code I've included is simplified in scope as a toy example: manual generation of all of the graphs would take too long. I'm a bit of a Latex newbie, so I really appreciate any insight!

Thank you!

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
TikZ book
User avatar
Stefan Kottwitz
Site Admin
Posts: 10312
Joined: Mon Mar 10, 2008 9:44 pm

Using Counter Value as Argument

Post by Stefan Kottwitz »

Welcome to the forum! You posted excellent minimal examples, easy to test, so I see a quick solution.

You could simply use \thea instead of \value{a}.

The prefix \the converts a counter into its printable form. When used with the counter's name, it displays its current value in the default format. Looks funny with just 'a', but perhaps you already know macros like \thesection and \thepage.

If it works for you and you have a nice complex generated graph, you may post it here because I'm sure that's beautiful and interesting!

Stefan
LaTeX.org admin
Post Reply