TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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?

Thanks in advance, Sean

\documentclass{article}
\usepackage{tabularx}

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

\newlength{\headerwidth}
\setlength{\headerwidth}{\textwidth}

%This table holds the document name.
\hspace{-6mm}
\begin{tabularx}{\headerwidth}{|c|X|c|} \hline
& \\
\footnotesize \sffamily{\textbf{Dokument-/Dateiname:}} & \\
\footnotesize \sffamily{\textbf{Document-/File-Name:}} & \raisebox{1\totalheight\relax}[0pt][0pt]{\docName} \\
\hline
\end{tabularx}
\end{document}
share|improve this question
    
Did you try \newline instead of \\? – Andrew Cashner yesterday
    
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 yesterday
    
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 yesterday

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}

enter image description here

share|improve this answer

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}
\newcommand{\HeaderBox}{%
  \par\noindent
  \begingroup
  \renewcommand{\tabularxcolumn}[1]{m{##1}}%
  \begin{tabularx}{\headerwidth}{|c|>{\raggedright}X|}
  \hline
  &\tabularnewline
  \begin{tabular}{@{}>{\footnotesize\sffamily\bfseries}l@{}}
  Dokument-/Dateiname: \\
  Document-/File-Name:
  \end{tabular} &
  \docName \tabularnewline
  &\tabularnewline
  \hline
  \end{tabularx}%
  \endgroup
  \par
}
\newlength{\headerwidth}
\AtBeginDocument{\setlength{\headerwidth}{\textwidth}}

\begin{document}
%This table holds the document name.
\HeaderBox

\end{document}

enter image description here

share|improve this answer

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}

\newlength{\headerwidth}
\setlength{\headerwidth}{\textwidth}
%This table holds the document name.
\hspace{-6mm}
\begin{tabularx}{\headerwidth}{|c|X|c|} \hline
%& \\
\footnotesize \sffamily{\textbf{Dokument-/Dateiname:}} & \\
\footnotesize \sffamily{\textbf{Document-/File-Name:}} & \DocName \\
\hline
\end{tabularx}
\end{document}

enter image description here

share|improve this answer
    
It worked on myexample, but needed futzing on my larger doc. I did run into an issue that results from an apparent oversimplification I made in my example, that I hope you can shed some light on. I am actually using a custom .cls file of mine based on article, and in it I have the following lines: \def\setDocName#1{\def\@setDocName{#1}} \newcommand{\DocName}{\@setDocName} if I replace the second by: \newcommand{\DocName}{\Longunderstack[l]{\@setDocName}} It doesn't work, but if I put the \Longunderstack in the actual document where I set the docname it does. Any thoughts? – Sean Cooper yesterday
    
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 yesterday
    
@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 yesterday
    
@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 yesterday

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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