# Trouble using convertchar and thestring inside a LaTeX command or table

I'm trying to generate page references inside a command that defines a row of a table, with the name of the label being referenced generated as a result of some string maniupulation using stringstrings. Here's a much simplified version of what I'm trying to do, generating a reference to the OPSFOO label based on a command parameter of FO_O, with the string processing removing underscores and prepending OPS:

\documentclass{article}
\usepackage{stringstrings}
\newcommand{\opind}[2]{
\convertchar[q]{OPS#2}{\_}{}
#1 & #2 & \pageref{\thestring} \\
}
\begin{document}
\begin{table}[h]
\begin{tabular}{l l l}
\opind{1}{FO\_O} \\
\end{tabular}
\end{table}
\clearpage
\label{OPSFOO} FOO \\
\end{document}


That fails with the error "! Undefined control sequence. \thestring"

I thought perhaps there was something wrong with using \thestring inside a command, so I tried putting it directly into the table entry like this:

\begin{table}[h]
\begin{tabular}{l l l}
\convertchar[q]{OPSFO\_O}{\_}{}
1 & FO\_O & \pageref{\thestring} \\
\end{tabular}
\end{table}


But that gets essentially the same error. So I started wondering if there's just a problem with using \thestring as an argument to \pageref. Apparently not; if I do the string manipulation outside the table, everything works fine:

\convertchar[q]{OPSFO\_O}{\_}{}
\begin{table}[h]
\begin{tabular}{l l l}
1 & FO\_O & \pageref{\thestring} \\
\end{tabular}
\end{table}


Is there anything I can do to get the string manipulation and the use of \pageref{\thestring} to work in the command that's called inside the table, as in the first code listing above?

• The problem seems to be, that \thestring is expanded locally in the first table cell, but then lost in the 3rd. one, since table cells are groups – user31729 Nov 1 '14 at 6:15
• Can you tell what you expect in argument #2? There can be much faster ways to solve your problem. Is lowercasing connected to this? Perhaps it's better if you state your complete problem. – egreg Nov 1 '14 at 9:56

According to the stringstrings documentation, \thestring is only a \edefined macro, valid only in the local group (a table cell here). To transport the content to the later table cells, it's necessary to make a global \edef, i.e. using an \xdef with some helper macro, say \theglobalstring

\documentclass{article}
\usepackage{stringstrings}

\newcommand{\opind}[2]{
\convertchar[q]{OPS#2}{\_}{}
\xdef\theglobalstring{\thestring}
#1 & #2 & \pageref{\theglobalstring} \\
}
\begin{document}
\begin{table}[h]
\begin{tabular}{l l l}
\opind{1}{FO\_O} \\
\end{tabular}
\end{table}
\label{OPSFOO} FOO \\

\clearpage
\end{document}

• There might be other ways of course ;-) – user31729 Nov 1 '14 at 6:19

Inspired by Christian Hupfer's comment to the problem, but avoiding using a global variable as in his answer:

\newcommand{\opind}[2]{
#1 & #2 & \convertchar[q]{OPS#2}{\_}{} \pageref{\thestring} \\
}

• This is also possible, but I thought, you would stick to \convertchar in the first cell. I see, that this is not necessary in your case – user31729 Nov 1 '14 at 6:23