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.

The following code does not work ??

\documentclass[a4paper]{book}

\usepackage{tabularx}

\begin{document}
\newcommand{\vrulehg}[1]
{\makebox[#1][c]{\rule{1pt}{0.5cm}}}% rule for vertical bar cell

\newcommand{\hrulehg}[3]{% hgrule{5cm}{c}{2}
\newdimen\wwo\wwo=#1%
\newdimen\wwl\wwl=\wwo\divide \wwl by #3%
\makebox[\wwo][#2]{\rule{\wwl}{1pt}}}

\begin{tabular}{ @{}>{\centering}p{6cm} @{}>{\centering}p{6cm} @{}>{\centering}p{6cm} @{}p{0.01cm} @{}>{\centering}p{6cm}X}
aaa & & bbb & & ccc \\
ddd & & eee & & fff \\
\end{tabular}
\end{document}

Error message is :

! Extra alignment tab has been changed to \cr.
<recently read> \endtemplate 

l.16 ddd &
           & eee & & fff \\
? 

I am using :

This is pdfTeX, Version 3.1415926-2.3-1.40.12 (TeX Live 2011)
 restricted \write18 enabled.
entering extended mode

on a Mac

Can someone help??

Thanks

share|improve this question
    
unrelated to your question but don't do \newdimen\wwo\wwo=#1% (or the other newdimen. this will allocate 2 dimensions wach time you use it (and there are only a finite number of those, 256 in classic tex) move the \newdimen statements to the preamble so you only allocate them once –  David Carlisle Jun 27 at 9:21

1 Answer 1

\\ is ambiguous: end of line inside cell or end of table row?

\tabularnewline explicitly ends a table row:

\begin{tabular}{ @{}>{\centering}p{6cm} @{}>{\centering}p{6cm}
@{}>{\centering}p{6cm} @{}p{0.01cm} @{}>{\centering}p{6cm}X}
aaa & & bbb & & ccc \tabularnewline
ddd & & eee & & fff \tabularnewline
\end{tabular}
\end{document}

The following new lines are available:

  • \tabularnewline: ends a table row to start a new one
  • \newline ends a line inside a cell (column type p)
  • \\: Usually it ends a table row unless

    • \centering, \raggedright, \raggedleft are used inside the column specification. These commands redefines \\ as \newline.

    • The latter can be reversed, if \arraybackslash is invoked after \centering or friends. The macro is provided by package array and redefines \\ to be \tabularnewline. Then\` ends a table row.

Example:

\documentclass{article}
\usepackage{array}
\begin{document}

\begin{tabular}{p{3cm} p{3cm}}
row 1, col 1, line 1\newline
row 1, col 1, line 2
&
row 1, col2, line 1\newline
row 1, col2, line 2
\\
\hline
row 2, col 1
&
row 2, col2
\tabularnewline
row 3, col 1
&
row 3, col2
\end{tabular}

\medskip

\begin{tabular}{>{\centering}p{3cm} >{\centering}p{3cm}}
row 1, col 1, line 1\\
row 1, col 1, line 2
&
row 1, col2, line 1\\
row 1, col2, line 2
\tabularnewline
\hline
row 2, col 1, line 1\newline
row 2, col 1, line 2
&
row 2, col2, line 1\newline
row 2, col2, line 2
\tabularnewline
\end{tabular}

\medskip

\begin{tabular}{>{\centering\arraybackslash}p{3cm}
                >{\centering\arraybackslash}p{3cm}}
row 1, col 1, line 1\newline
row 1, col 1, line 2
&
row 1, col2, line 1\newline
row 1, col2, line 2
\\
\hline
row 2, col 1, line 1\newline
row 2, col 1, line 2
&
row 2, col2, line 1\newline
row 2, col2, line 2
\tabularnewline
\hline
row 3, col 1
&
row 3, col2
\tabularnewline
\end{tabular}

\end{document}

Result example

share|improve this answer
    
Or use \centering\arraybackslash in the table preamble. –  daleif Jun 27 at 6:18

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.