2

How to \emph a \csname\command\endcsname?? I tried to use \csgdef and it works but not for \csname and I need to use \csxdef.

\documentclass[a4paper,12pt,hidelinks]{article}

\usepackage[utf8]{inputenc} 
\usepackage[brazil]{babel}
\usepackage[T1]{fontenc}

\usepackage{filecontents}
\usepackage{csvsimple}
\usepackage{xstring}
\usepackage{alphalph}
\usepackage{etoolbox}
\usepackage{tikz}

\newcommand \garotab {garota}
\newcommand \nveics {2}

\foreach \x [count=\xi from 2] in {b,...,\alphalph{\nveics}}{
% \csxdef{teste\x}{\emph{\csname garota\x\endcsname}}

\csxdef{rato\x}{\em ah garota}
\csgdef{gato\x}{\emph{ah garota}}
}

\begin{document}

{\testeb}\\

{\gatob}\\

{\ratob}\\

\end{document}
1
  • 1
    \noexpand\emph
    – egreg
    Apr 8 '16 at 16:45
2

You need to avoid expanding \emph, so use \noexpand in front of it.

\documentclass{article}

\usepackage{alphalph}
\usepackage{etoolbox}
\usepackage{tikz}

\newcommand \garotab {garota}
\newcommand \nveics {2}

\foreach \x [count=\xi from 2] in {b,...,\alphalph{\nveics}}{
  \csxdef{teste\x}{\noexpand\emph{\expandonce{\csname garota\x\endcsname}}}
  \csxdef{rato\x}{\noexpand\em ah garota}
  \csgdef{gato\x}{\emph{ah garota}}
}

\begin{document}

{\testeb}  

{\gatob}  

{\ratob}  

\end{document}

Note that in this way you have performed the same as

\def\testeb{\emph{\garotab}}

If you instead want to fully expand \garotab (which I'm not sure you do), change the line into

  \csxdef{teste\x}{\noexpand\emph{\csname garota\x\endcsname}}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.