# xstring StrSubstitute and table

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.

\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}

\providecommand{\test}[1][0pt]{%
Test1 & Test\newline Test2\tabularnewline
\saveexpandmode\expandarg
\restoreexpandmode
\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

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:

## 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}

\saveexpandmode\expandarg
}%

\providecommand{\test}[1][0pt]{%
Test1 & Test\newline Test2\tabularnewline