Take the 2-minute tour ×
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.

Is there a way to put \\ after multicolumn automatically, each time it appears in the code? Pseudocode would look like this:

\newcommand{\commandThatAddsBreaklines}
if(thereIsAMulticolumn == true)
    add '\\' after \multicolumn{}{}{a very long text that doesn´t even fit in my table}

I would like to know if it is even possible to do something like this, maybe someone has a idea. My code looks like this:

\documentclass[a4paper,12pt,oneside]{scrbook}
\usepackage[dutch]{babel}  %Quotes won't work without babel
\usepackage[utf8]{inputenc}   %This is very important!
\usepackage[T1]{fontenc}

\usepackage{graphicx}
\usepackage[pdfborder={0 0 0}, breaklinks=true, pdftex=true, raiselinks=true]{hyperref}
\usepackage{tabularx}
\newcolumntype{Y}{>{\raggedright}X}

\begin{document}
\section{Description}
\begin{tabularx}{\linewidth}{YY}\hline
\multicolumn{2}{c}{a very very very long text that doesn´t even fit in my table, so it must be a way to make the table automatically break the line}\\
\end{tabularx}
\end{document}

I am also looking for a way to fit the column into my table. It would be nice if someone could provide me some help with it, thanks in advance.

share|improve this question
    
What's the advantage with respect to adding the \\ manually? –  egreg Nov 22 '12 at 14:33
    
I need to edit the .tex file from .sty and since the .tex file is generated automatically, and doesn´t have any \\ it is usefull to add some to avoid errors –  dima Nov 22 '12 at 14:42
    
I don't understand. If the automatic file generation doesn't add it, then it's the generation to be wrong. Is the code a sample of a generated file? –  egreg Nov 22 '12 at 14:57
    
The code sample is from the generated file except for the line \multicolumn{2}{c}{text}\\, in the autogenerated file the command looks like this: \multicolumn{}{}{text}, and in this case it does´t compiles. The generating of the file is done by Wiki2LaTeX extension of the MediaWiki. –  dima Nov 22 '12 at 15:08
add comment

1 Answer

up vote 4 down vote accepted

enter image description here

\documentclass[a4paper,12pt,oneside]{scrbook}
\usepackage[dutch]{babel}  %Quotes won't work without babel
%\usepackage[utf8]{inputenc}   %This is very important!
\usepackage[T1]{fontenc}


\usepackage{graphicx}
\usepackage[pdfborder={0 0 0}, breaklinks=true, pdftex=true, raiselinks=true]{hyperref}
\usepackage{tabularx}
\newcolumntype{Y}{>{\raggedright\arraybackslash}X}

\let\oldmc\multicolumn
\def\multicolumn#1#2#3{%
\oldmc{#1}{>{\hsize=\dimexpr#1\hsize+#1\tabcolsep+#1\tabcolsep-2\tabcolsep\relax\centering}X}{#3}\tabularnewline}

\begin{document}
\section{Description}

\noindent
\begin{tabularx}{\linewidth}{YY}\hline
\multicolumn{2}{c}{a very very very long text that doesn´t even fit in my table, so it must be a way to make the table automatically break the line}
\end{tabularx}
\end{document}
share|improve this answer
    
Thanks for help –  dima Nov 23 '12 at 6:43
    
I only have one more question, what does this greater than symbol do in this line: \oldmc{#1}{>{\hsize=\dimexpr#1\hsize+#1\tabcolsep+#1\tabcolsep-2\tabcolsep\relax‌​\centering}X}{#3}\tabularnewline}? –  dima Nov 23 '12 at 6:52
    
It's just array package column specifier the same as you used in your definition of Y to push those statements in to the start of the cell. –  David Carlisle Nov 23 '12 at 9:18
    
The line with \newcolumntype was generated from a parser, so I wanted to know what this > means. Now i understand, thanks a lot. –  dima Nov 23 '12 at 10:14
    
Oh in that case you should report a bug in the generator, you'll note that I changed it to add \arraybackslash as it was it will work in all columns but the last, if use the definition og Y in your example in the last column (as you have done) then \` will mean new line in raggedright` not end of table row so you would have to end table rows with \tabularnewline rather than \\ \arraybackslash resores the meaning of \\ to its array/tabular end of line meaning. –  David Carlisle Nov 23 '12 at 10:19
show 1 more comment

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.