1

I have had this problem before (sort of) - it's related to this question I asked previously.

Basically, I am having trouble with my table headings. I want the headings to be bold and centered regardless of the alignment of the text in the columns below. I have defined a new command head to do this, which seems to be working fine except for the last column. If I use head in place of \bfseries in the last column I get an error

! Misplaced \noalign. \cmidrule ->\noalign 
                     {\ifnum 0=`}\fi \@ifnextchar [{\@cmidrule }{\@cmidrule ... l.37 \cmidrule
              (r){1-1} \cmidrule(lr){2-3} \cmidrule(lr){4-5} \cmidrule(l){6-...

I have read various things about "mislpaced \noalign" complaints coming from things to do with backslashes, but I don't see why that last column should behave any differently from the others.

I have tried to get the text centered by adding a \centering instead but the required \par messes up the vertical alignment. Anyway, I don't see that I should have to do something different, but I can't see what I've done wrong?

\documentclass[11pt, a4paper, twoside, fleqn]{report}
\usepackage[showframe]{geometry}

\makeatletter
\g@addto@macro\@floatboxreset\centering % automatically center floats
\makeatother


%Table formatting
%------------------------------
\usepackage{array}
\usepackage{booktabs}
\renewcommand{\arraystretch}{1.5}
\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{R}[1]{>{\raggedleft\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}

\newcommand{\head}[1]{\centering\textbf{#1}}

%Definitions of useful shortcuts (e.g. to add matrix formatting)
%---------------------------------------------------------------


\begin{document}         

\begin{table}

\footnotesize
%%%\addtolength{\tabcolsep}{-0.6pt}

\begin{tabular}{ @{} L{1.5cm}  C{1.5cm} C{2cm} C{1.5cm} C{2cm} L{4cm} @{}}

\toprule

\head{Head}  & \head{Stuff} & \head{Things}  & \head{Stuff} & \head{Things} & \bfseries Stuff Things \\
\head{Head}  & \head{Stuff} & \head{Things}  & \head{Stuff} & \head{Things} & {\head{Stuff Things}} \\

\cmidrule(r){1-1} \cmidrule(lr){2-3} \cmidrule(lr){4-5} \cmidrule(l){6-6} % no "r" here because it's the last   

\multicolumn{2}{@{}l}{\bfseries Subheading}\\% \addlinespce

text & text & some much longer text & text &   some much longer text & some even longer even longer even longer text \\

\bottomrule
\end{tabular}

\caption{And here's the caption}
\end{table}




\end{document}

Update:

\listfiles gives the following:

  report.cls    2005/09/16 v1.4f Standard LaTeX document class<br>
   fleqn.clo    1998/08/17 v1.1c Standard LaTeX option (flush left equations)<br>
  size11.clo    2005/09/16 v1.4f Standard LaTeX file (size option)<br>
   array.sty    2005/08/23 v2.4b Tabular extension package (FMi)<br>
booktabs.sty    2005/03/16 v1.6180 publication quality tables

We are well aware that our latex is very very out of date! Something I am trying to get fixed. I can overwrite locally if necessary.

Just to be clear, the compilation problem arises if I replace

& \head{Stuff} & \head{Things}  & \head{Stuff} & \head{Things} & {\bfseries Stuff Things} \\

with

& \head{Stuff} & \head{Things}  & \head{Stuff} & \head{Things} & \head{Stuff Things} \\

Update 2

I've edited my code above to make it more obvious that this problem is only arising the last column (the first column is L aligned too, but the \head command is centering my heading).

As per Johannes_B's suggestion, if I add an extra set of braces to the last column

& \head{Stuff} & \head{Things}  & \head{Stuff} & \head{Things} & {\head{Stuff Things}} \\

the compilation problem goes away but the final column heading remains left aligned:

table

Using makecell and in particular thead seems to work fine, but I'd still like to know what's wrong with my code! Can anyone tell me why I am getting this behaviour?

14
  • Why those \let\newline\\? Remove them and it compiles just fine
    – daleif
    Aug 6, 2014 at 13:44
  • @daleif but then how would I insert a line break into a table cell? Perhaps there is some obvious other method that I have yet to come across
    – FionaSmith
    Aug 6, 2014 at 13:52
  • \arraybackslash takes care of that.
    – Johannes_B
    Aug 6, 2014 at 13:55
  • @Johannes_B could you expand on that please?
    – FionaSmith
    Aug 6, 2014 at 13:56
  • Just as @daleif said, leave out what he mentioned. You can insert a linebreak using ` \\ ` because you have \arraybackslash in your definition.
    – Johannes_B
    Aug 6, 2014 at 13:57

1 Answer 1

3

Use the makecell package: it defines, in particular, a \thead and \makecell commands that suppport common formatting and linebreaks inside cells. By default, the contents of cells are centred both vertically and horizontally. Also your table goes into the margin as it is. I changed the environment to tabularx and made the last column of X type, redefined so as to have cells vertically centred.

\documentclass[11pt, a4paper, twoside, fleqn]{report}

\makeatletter
\g@addto@macro\@floatboxreset\centering % automatically center floats
\makeatother

%Table formatting
%------------------------------
\usepackage{array}
\usepackage{booktabs}
\renewcommand{\arraystretch}{1.5}
\newcolumntype{L}[1]{>{\raggedright\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{C}[1]{>{\centering\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{R}[1]{>{\raggedleft\arraybackslash\hspace{0pt}}m{#1}}

\newcommand{\head}[1]{\hfill\textbf{#1}\hfill\mbox{}}
\usepackage{makecell, tabularx}
\renewcommand\theadfont{\bfseries}
\renewcommand{\tabularxcolumn}[1]{>{\small}m{#1}}
%---------------------------------------------------------------

\usepackage[nomarginpar,  showframe]{geometry}

\begin{document}

\begin{table}

\footnotesize
\begin{tabularx}{\linewidth}{ @{} L{1.5cm}  C{1.5cm} C{2cm} C{1.5cm} C{2cm}X @{}}

\toprule
  \multicolumn{1}{c}{\bfseries Something} & \multicolumn{2}{c}{\bfseries Something Else} &  \multicolumn{2}{c}{\bfseries Something Else Entirely} &  \\

  & \thead{Stuff} & \thead{Things}  & \thead{Stuff} & \thead{Things} & \head{ Stuff Things} \\

\cmidrule(r){1-1} \cmidrule(lr){2-3} \cmidrule(lr){4-5} \cmidrule(l){6-6} % no "r" here because it's the last

\multicolumn{2}{@{}l}{\bfseries Subheading}\\% \addlinespce

text & text & some much longer text & text &   some much longer text & some even longer even longer even longer text \\

\midrule

\multicolumn{2}{@{}l}{\bfseries Subheading 2}\\% \addlinespce

text & text & some much longer text & text &   some much longer text & some even longer even longer even longer text \\

\bottomrule
\end{tabularx}

\caption{And here's the caption}
\end{table}

\end{document} 

enter image description here

** Comment on the O.P.'s code:**

You do not need \let\newline\\, nor the pair of braces around the last \head . The problem in the last column seems to come from the fact that you change the alignment in a cell of the last column. I see two solutions (workarounds?):

  1. In the ‘head’ row, replace \\ with \tabularnewline.
  2. Or keep \\, but replace the definition of head with this one:

    \newcommand{\head}[1]{\hfill\textbf{#1}\hfill\mbox{}}

enter image description here

6
  • Thanks very much for your suggestion. Using \thead does indeed seem to solve all my problems, and is probably the simplest solution generally, so I will almost certainly go with that. Curiously, the table was not overhanging the margins for me! Anyway, it's obviously just a mock-up not the real thing, so I don't really need to use tabularx. However, I still want to know why my code is not working, so I will leave this open for a bit to see if someone can explain.
    – FionaSmith
    Aug 7, 2014 at 9:26
  • Right, nobody else seems to have time or inclination to work out what's wrong, and the use of \thead solves the problem nicely and neatly, so thank you very much for your answer, which I have now accepted!
    – FionaSmith
    Aug 12, 2014 at 8:51
  • I've added comments on your code, and two possible workarounds.
    – Bernard
    Aug 12, 2014 at 10:23
  • Thanks very much @Bernard, I still don't understand why it should be just the last column though - the first column works fine and is also L aligned
    – FionaSmith
    Aug 12, 2014 at 10:47
  • I don't understand very well myself, but it's clearly linked to the end of row (\\') and the way \centeringor \ragged…` work. I obtain a misplaced \noalign in the .log. Unfortunately I don't know the internals of these commands.
    – Bernard
    Aug 12, 2014 at 11:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .