2

I got a problem with xstring \StrSubstitute in combination with a table. I would like to replace newline (\\) in a variable with the \newline command to generate a second line in a paragraphed table cell. Therefore I got the following code, which works fine for the first output (if I put \addr to the left column), but for the second output in the right column I get a

Undefined control sequence error

I have no idea how to fix this and why the first output works but not the second.

Thanks for any help.

edit: added MWE

\documentclass{article}

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

\def\address{Test123\\Test234}

\providecommand{\test}[1][0pt]{%
Test1 & Test\newline Test2\tabularnewline
\ifx\@empty\address\else
\saveexpandmode\expandarg
\StrSubstitute{\address}{\empty\\}{\empty\newline}[\addr]%
\restoreexpandmode
\addr & Address \tabularnewline
Address    & \addr   \tabularnewline
\fi
}   
\begin{document}

\begin{tabular}{p{2cm}|p{2cm}}
\test
\end{tabular}


\end{document}
  • Welcome to TeX.SX! Please add a minimal working example (MWE) that illustrates your problem. It will be much easier for us to reproduce your situation and find out what the issue is when we see compilable code, starting with \documentclass{...} and ending with \end{document}. – Marco Daniel Jul 6 '13 at 16:45
1

The problem is that assignments within cells of a table are local to that cell. So, one way around that is to define a macro to generate the desired result:

enter image description here

Code:

\documentclass{article}

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

\def\address{Test123\\Test234}

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

\providecommand{\test}[1][0pt]{%
Test1 & Test\newline Test2\tabularnewline
\addrGlobal & Address \tabularnewline
Address    &  \addrGlobal   \tabularnewline
\fi
}   
\begin{document}

\begin{tabular}{p{2cm}|p{2cm}}
\test
\end{tabular}


\end{document}

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.