3

I would like to debug a latex macro, but I do not really know how. I got the following MWE which replaces the \\ in a string. I would like to print the value of generated string to check the result. The first output of \show is as expected \textbf {Test123\\Test234}. Now I would like to output the macro \addrGlobal, so that it shows \textbf {Test123\newline Test234}, but it does not work as expected. How can this output be archieved?

Thanks in advance!

\documentclass{article}

\usepackage[a4paper,top=1.27cm,left=1cm,right=1cm,bottom=2cm]{geometry}
\usepackage{ifpdf}
\usepackage[utf8x]{inputenc}
\usepackage{xstring}


\def\address{\textbf{Test123\\Test234}}

\newcommand{\addrGlobal}{%
\ifx\@empty\address\else
\saveexpandmode\expandarg
\StrSubstitute{\address}{\empty\\}{\empty\newline}[\addr]%
\restoreexpandmode\addr%
}%

\relax
\show\address
\show\addrGlobal


\begin{document}

\end{document}
  • You should add \usepackage{xstring}, and then I think you want to look at \addr (after invoking \addrGlobal), not \addrGlobal. – Peter Grill Jul 20 '13 at 17:29
3

There are various issues in your code.

  1. Why checking whether \address is empty? You have just defined it not to be
  2. \makeatletter and \makeatother are missing around the code that uses \@empty
  3. The string \empty\\ doesn't appear in \address, so nothing is changed.
  4. \addrGlobal will not contain a string, but rather \addr, when \addrGlobal is executed, which isn't in your code. Until you don't issue \addrGlobal, no value will be given to \addr.

Here's a possible fix.

\documentclass{article}

\usepackage{xstring}

\def\address{\textbf{Test123\\Test234}}

\newcommand{\fixaddress}{%
  \saveexpandmode\expandarg\exploregroups
  \StrSubstitute{\address}{\noexpand\\}{\newline}[\addrGlobal]%
  \restoreexpandmode
}    

\show\address
\fixaddress
\show\addrGlobal

Note \noexpand\\ in order not to expand the string to be searched and \exploregroups. The \ifx\@empty\address test is useless, because if the string is empty, no substitution will be performed.

Here is the output on the terminal:

> \address=macro:
->\textbf {Test123\\Test234}.
l.13 \show\address

?
> \addrGlobal=macro:
->\textbf {Test123\protect \newline  Test234}.
l.15 \show\addrGlobal

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.