Getting \csname to ignore subscript symbol

\documentclass[a4paper,11pt]{book}

% I have several commands with the same prefix
\newcommand{\fooT}{T}
\newcommand{\fooS}{S}

% I want to call these commands from another command with respect to the given parameter
\newcommand{\fooL}[1]{\fooLhelp#1\relax}
\def\fooLhelp#1#2\relax{L_{\csname foo#1\endcsname#2}}

\begin{document}
Without subscript $\fooL{T}$ gives the same thing than $L_{\fooT}$.


With subscript $\fooL{T_j}$ is different from $L_{\fooT_j}$.
\end{document}

enter image description here

Manuel's suggestion streamlines the approach still further:

\newcommand*\fooL[1]{L_{\auxfoo#1}}
\newcommand*\auxfoo[1]{\csname foo#1\endcsname}

since \auxfoo will only grab the first token of the argument.


ADDENDUM

Some comment discussion occurred for cases where the underlying \foox macro occurs when x is more than a single token. Manuel noted that such cases can be addressed in a limited way, under the proviso that underlying macro only appears by itself or followed by a subscript.

Here is how that would be done:

\documentclass[a4paper,11pt]{book}

% I have several commands with the same prefix
\newcommand{\fooT}{T}
\newcommand{\fooTTT}{TTT}
\newcommand{\fooS}{S}

% I want to call these commands from another command with respect to the given parameter
\newcommand{\fooL}[1]{\fooLhelp#1_\relax}
\def\fooLhelp#1_#2\relax{L_{\csname foo#1\endcsname\ifx\relax#2\relax\else_\foolHelp#2\fi}}
\def\foolHelp#1_{#1}

\begin{document}
Without subscript $\fooL{T}$ gives the same thing than $L_{\fooT}$.


With subscript $\fooL{T_j}$ is different from $L_{\fooT_j}$.


With longer base $\fooL{TTT}$ and subscript $\fooL{TTT_j}$.
\end{document}

enter image description here


When using \csname you always risk to get unexpected results, so I add also error checking.

\documentclass{article}

\newcommand{\fooT}{\mathcal{T}}
\newcommand{\fooS}{\mathbf{S}}
\newcommand{\fooST}{\mathrm{ST}}

\makeatletter
\newcommand{\fooL}[1]{\fooL@aux#1_\fooL@aux}
\def\fooL@aux#1_#2\fooL@aux{%
  \ifcsname foo#1\endcsname
    \csname foo#1\endcsname
  \else
    \@latex@error{Wrong argument}{The 'foo#1' command does not exist}%
  \fi
  \if\relax\detokenize{#2}\relax
    % no _ in the argument
  \else
    \fooL@aux@i#2% remove the surplus _
  \fi
}
\def\fooL@aux@i#1_{_{#1}}
\makeatother

\begin{document}
$\fooL{T}$ and $\fooL{T_j}$

$\fooL{S}$ and $\fooL{S_jkl}$

$\fooL{ST}$ and $\fooL{ST_j}$

$\fooL{X}$
\end{document}

This gives

enter image description here

and also the following messages on the console:

! LaTeX Error: Wrong argument.

See the LaTeX manual or LaTeX Companion for explanation.
Type  H <return>  for immediate help.
 ...                                              

l.31 $\fooL{X}
              $
? h
The 'fooX' command does not exist
? 

An expl3 implementation:

\documentclass{article}
\usepackage{xparse}

\newcommand{\fooT}{\mathcal{T}}
\newcommand{\fooS}{\mathbf{S}}
\newcommand{\fooST}{\mathrm{ST}}

% look for _ in the argument    
\NewDocumentCommand{\fooL}{>{\SplitArgument{1}{_}}m}{\fooLaux#1}

\ExplSyntaxOn
\NewDocumentCommand{\fooLaux}{mm}
 {
  \cs_if_exist:cTF { foo#1 }
   {
    \use:c { foo#1 }
   }
   {
    \msg_error:nnn { mp } { foo-not-exist } { #1 }
   }
  % if _ is found, argument #2 has value
  \IfValueT{#2}{\sb{#2}}
 }
% define the error message
\msg_new:nnnn { mp } { foo-not-exist }
 {
  Wrong~argument
 }
 {
  The~command~'foo#1'~does~not~exist
 }
\ExplSyntaxOff

\begin{document}
$\fooL{T}$ and $\fooL{T_j}$

$\fooL{S}$ and $\fooL{S_jkl}$

$\fooL{ST}$ and $\fooL{ST_j}$

$\fooL{X}$
\end{document}

Tags:

Symbols

Macros