\ifthenelse within \caption, \listoftables does not work

EXAMPLE 1 below works fine but EXAMPLE 2 produces "undefined control sequence". The only difference is the \vlabel command within brackets in the \caption command. What is the problem?

EXAMPLE 1:

\documentclass{article}
\usepackage{pgffor}
\usepackage{ifthen}

\newcommand{\vlabel}[1]
{%
\ifthenelse{\equal{#1}{numbersa}}{Numbers A}{}%
}

\begin{document}

\listoftables

\begin{table}
\caption[Numbers A]{\vlabel{numbersa}}
\end{table}

\end{document}


EXAMPLE 2:

\documentclass{article}
\usepackage{pgffor}
\usepackage{ifthen}

\newcommand{\vlabel}[1]
{%
\ifthenelse{\equal{#1}{numbersa}}{Numbers A}{}%
}

\begin{document}

\listoftables

\begin{table}
\caption[\vlabel{numbersa}]{\vlabel{numbersa}}
\end{table}

\end{document}

• Hi and welcome, Code highlighting and markup works just like over at TeXwelt.de. – Johannes_B Mar 30 '15 at 18:23

The expansion of ifthen macros causes problems in the auxiliary files. You either need to \protect them

...
\caption[\protect\vlabel{numbersa}]{\vlabel{numbersa}}
...


or use an alternative:

\documentclass{article}
\newcommand{\vlabel}[1]{%
\ifnum\pdfstrcmp{#1}{numbersa}=0
Numbers A%
\fi
}

\begin{document}

\listoftables% LoF

\begin{table} \caption[Numbers A]{\vlabel{numbersa}} \end{table}

\begin{table} \caption[\vlabel{numbersa}]{\vlabel{numbersa}} \end{table}

\end{document}


\pdfstrcmp (requires e-TeX) is expandable, making sure that only Numbers A makes its way into the LoF.

• Excellent, both solutions work perfectly, thank you very much! – Sarine Mar 30 '15 at 21:11
{"extraction_info": {"found_math": true, "script_math_tex": 0, "script_math_asciimath": 0, "math_annotations": 0, "math_alttext": 0, "mathml": 0, "mathjax_tag": 0, "mathjax_inline_tex": 0, "mathjax_display_tex": 0, "mathjax_asciimath": 1, "img_math": 0, "codecogs_latex": 0, "wp_latex": 0, "mimetex.cgi": 0, "/images/math/codecogs": 0, "mathtex.cgi": 0, "katex": 0, "math-container": 0, "wp-katex-eq": 0, "align": 0, "equation": 0, "x-ck12": 0, "texerror": 0, "math_score": 0.8661720752716064, "perplexity": 13606.814869333362}, "config": {"markdown_headings": false, "markdown_code": true, "boilerplate_config": {"ratio_threshold": 0.18, "absolute_threshold": 10, "end_threshold": 15, "enable": true}, "remove_buttons": true, "remove_image_figures": true, "remove_link_clusters": true, "table_config": {"min_rows": 2, "min_cols": 3, "format": "plain"}, "remove_chinese": true, "remove_edit_buttons": true, "extract_latex": true}, "warc_path": "s3://commoncrawl/crawl-data/CC-MAIN-2020-34/segments/1596439736057.87/warc/CC-MAIN-20200810145103-20200810175103-00335.warc.gz"}