# How to handle input strings in tables, when the input string has line breaks in it?

I have a document template I've written that has worked well until now. It includes input to define things like the name of the document, doc control numbers, etc.

Unfortunately, I have a case of a document with a multi-line title, which is not processing correctly and I can't figure out how to detect it and/or process it other than by making a special exception (which I'd rather not do if I can help it [plus this is a chance to expand my understand of latex ;) ]).

The example code below runs fine, but if you remove the %comment% demarcator from the middle of line 5, it errors out. Does anyone know how I can get latex to detect this case and put the two parts of the docName on separate rows of the table?

\documentclass{article}
\usepackage{tabularx}

\begin{document}
\newcommand{\docName}{Video Endoscope \& Imaging Module}% \\DVT Support Manual}

%This table holds the document name.
\hspace{-6mm}
& \\
\footnotesize \sffamily{\textbf{Dokument-/Dateiname:}} & \\
\footnotesize \sffamily{\textbf{Document-/File-Name:}} & \raisebox{1\totalheight\relax}[0pt][0pt]{\docName} \\
\hline
\end{tabularx}
\end{document}

• Did you try \newline instead of \\? – musarithmia May 4 '16 at 15:45
• I had not.... , but given your suggestion I did. In the sample code above, both versions error out. In a larger document (>100pages) that I am using, the \\ didn't error out in either case, but with \\ it put the second line one row down and one column left. With \newline it put it one row down in the same column (which is improved, but distends the table oddly. – Sean Cooper May 4 '16 at 15:57
• Thank you all for such diverse and interesting answers! I am trying each of them out to see how they work. I hope you don't mind, but I'm probably going to pepper all of you with questions as I try to figure out 1) if/how they work and 2) which is the best fit for my case. :) Y'all are awesome! – Sean Cooper May 4 '16 at 16:41

Use a \Longunderstack or a \Longstack.

EDITED to handle case where a macro containing the data is to be set in a stack (per the OP's comments). In this case, the line

\setDocName{Video Endoscope \& Imaging Module \\DVT Support Manual}


places the data in the variable \@setDocName. At a later point, the contents of \@setDocName are to be set in a \Longunderstack.

\documentclass{article}
\usepackage{tabularx,stackengine}
\setstackEOL{\\}
\makeatletter
\def\setDocName#1{\def\@setDocName{#1}}
\newcommand{\DocName}{%
\def\tmp{\Longunderstack[l]}%
\expandafter\tmp\expandafter{\@setDocName}%
}
\makeatother
\begin{document}
\setDocName{Video Endoscope \& Imaging Module \\DVT Support Manual}

%This table holds the document name.
\hspace{-6mm}
%& \\
\footnotesize \sffamily{\textbf{Dokument-/Dateiname:}} & \\
\footnotesize \sffamily{\textbf{Document-/File-Name:}} & \DocName \\
\hline
\end{tabularx}
\end{document}


• I also notice that the \Longunderstack sets the centering. I use this in several places and would ideally like to use left-centering in some and center-centering in others. Is there an easy way to do that? [short of just defining two different versions anyway :)] – Sean Cooper May 4 '16 at 17:04
• @SeanCooper To the first question, \newcommand{\DocName}{\def\tmp{\Longunderstack[l]} \expandafter\tmp\expandafter{\@setDocName}} works. I can edit my answer to reflect that if you want. – Steven B. Segletes May 4 '16 at 17:06
• @SeanCooper The default centering for a stack is centered. The optional argument [l] overrides the default [c] and makes it left-aligned. – Steven B. Segletes May 4 '16 at 17:08
• Thank you very much for the update. It does nearly exactly what I hoped for (and I was easily able to adjust it by putting the [l] or [c] parameter as a pass through input to \DocName. It did introduce an error, that I successfully worked around by changing the order of my code, but if possible I would like to understand what happened. The issue was that other functions equivalent to \DocName defined later started erroring. I moved them beforehand and they work. Any thoughts? – Sean Cooper May 6 '16 at 19:11

Use a tabular inside the tabularx:

\documentclass{article}
\usepackage{tabularx}
\begin{document}
\newcommand\docName{\tabular[t]{@{}l@{}}Video Endoscope \& Imaging Module \\
DVT Support Manual\endtabular}
\noindent
\begin{tabularx}{\textwidth}{|c|X|} \hline
&\\
\footnotesize\sffamily\bfseries
\tabular[t]{@{}l@{}}Dokument-/Dateiname:\\Document-/File-Name:\endtabular & \docName \\
&\\\hline
\end{tabularx}
\end{document}


You can use \raggedright, that suddenly allows \\ in \docName, but requires terminating the rows with \tabularnewline; not a big deal, as this table can be defined in the preamble.

\documentclass{article}
\usepackage{tabularx}

\newcommand{\docName}{Video Endoscope \& Imaging Module \\ DVT Support Manual}
\par\noindent
\begingroup
\renewcommand{\tabularxcolumn}[1]{m{##1}}%
\hline
&\tabularnewline
\begin{tabular}{@{}>{\footnotesize\sffamily\bfseries}l@{}}
Dokument-/Dateiname: \\
Document-/File-Name:
\end{tabular} &
\docName \tabularnewline
&\tabularnewline
\hline
\end{tabularx}%
\endgroup
\par
}