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.

This question already has an answer here:

I already asked how to best align header rows in a table especially in a tabu environment.

Align (center) header row in table

The two identical solutions worked great for one table but I am having issues with another table now. Using exactly the same technique I cannot get the headers aligned properly. Here is my example plus a screenshot of the table below:

\documentclass[10pt,DIV=12,a4paper,numbers=noenddot]{scrreprt}

\usepackage{tabu}
\usepackage{multicol}
\usepackage{multirow}
\usepackage{longtable}
\usepackage{caption}
\usepackage{booktabs}

\usepackage{etoolbox}
\makeatletter
\newcommand{\changefont}[3]{\usefont{\f@encoding}{#1}{#2}{#3}}
\patchcmd{\LT@makecaption}{\hss}{\hss\normalfont\normalsize}{}{}
\makeatother
\newenvironment{myfont}[6][1.2] % 1.2 is the default for the baselinestretch
  {\par\linespread{#1}\fontsize{#5}{#6}\changefont{#2}{#3}{#4}}
  {\par}

\linespread{1.2}

\begin{document}

\begin{table}

\begin{myfont}{phv}{m}{n}{6}{9}

\begin{tabu}{l@{\hspace*{43pt}}r@{\hspace*{43pt}}r@{\hspace*{15pt}}r@{\hspace*{42pt}}rr@{\hspace*{42pt}}rr}

\multicolumn{1}{c}{column1} & \multicolumn{1}{c}{CC} & \multicolumn{1}{c}{column2} &
\multicolumn{1}{l}{column3} & \multicolumn{2}{c}{column4} & 
\multicolumn{2}{c}{{\hspace*{12pt}}column5} \\

% \multicolumn{2}{c@{\hspace*{12pt}}}{column5} \\
% \multicolumn{1}{l}{column3{\hspace*{10pt}}}

    &   &   &   & AA & BB & AA & BB \\

\midrule
AAAAAAAAAAAA & BBBBBBB & BBBBBBB & BBBBBBB & BBBBBBB  & BBBBBBB  & BBBBBBB  & BBBBBBB \\
AAAAAAAAAAAA & BBBBBBB & BBBBBBB & BBBBBBB & BBBBBBB  & BBBBBBB  & BBBBBBB  & BBBBBBB \\
AAAAAAAAAAAA & BBBBBBB & BBBBBBB & BBBBBBB & BBBBBBB  & BBBBBBB  & BBBBBBB  & BBBBBBB \\
\end{tabu} 
\end{myfont}
\end{table}

\end{document}

enter image description here

From the screenshot one can clearly see that all the centred {c} columns are not in line with the rest of the column. I used the \hspace approach from my previous question to center column 5 again using \multicolumn{2}{c}{{\hspace*{12pt}}column5}. However, this seemed to have an effect only when the \hspace is inside the curly brackets of column5 and not within the {c} column unlike in my previous question. I commented out this approach after the column definitions to show you how I tried to do it. I did not forget to add the @ sign when I put it within the {c} column.

Then I tried to center column 3. I aligned it left because even when it was aligned left it was already too far to the right hand side. So I added some \hspace after the column name column3 to shift it back to the left. See commented line: % \multicolumn{1}{l}{column3{\hspace*{10pt}}}. However, nothing happened until I added 40 or more pt's. Then suddenly, instead of moving the column header latex shifted all columns from column 3 onwards to the right leaving the headers where they were before.

The same happened to the other columns. Sometimes the header did not move. Sometimes they moved but sometimes just the rest of the column got shifted. I think I was just lucky with my previous example that it worked there. It seems so arbitrary how latex handles that spacing or does it only look arbitrary to me? Can anyone help to properly align these header rows, please?

share|improve this question

marked as duplicate by David Carlisle, zeroth, lockstep, Claudio Fiandrino, bloodworks Feb 8 '13 at 9:35

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

add comment

1 Answer 1

enter image description here

This is an exact duplicate of the previous question, I put an answer here but we should close it as a duplicate, as stated there you need to use @. Also as said in the earlier questions using @ this way to manually adjust the table width to the page size is a terrible error prone technique when tex can do it automatically.

\documentclass[10pt,DIV=12,a4paper,numbers=noenddot]{scrreprt}

\usepackage{tabu}
\usepackage{multicol}
\usepackage{multirow}
\usepackage{longtable}
\usepackage{caption}
\usepackage{booktabs}

\usepackage{etoolbox}
\makeatletter
\newcommand{\changefont}[3]{\usefont{\f@encoding}{#1}{#2}{#3}}
\patchcmd{\LT@makecaption}{\hss}{\hss\normalfont\normalsize}{}{}
\makeatother
\newenvironment{myfont}[6][1.2] % 1.2 is the default for the baselinestretch
  {\par\linespread{#1}\fontsize{#5}{#6}\changefont{#2}{#3}{#4}}
  {\par}

\linespread{1.2}

\begin{document}

\begin{table}

\begin{myfont}{phv}{m}{n}{6}{9}

\begin{tabu}{l@{\hspace*{43pt}}r@{\hspace*{43pt}}r@{\hspace*{15pt}}r@{\hspace*{42pt}}rr@{\hspace*{42pt}}rr}

\multicolumn{1}{c@{\hspace*{43pt}}}{column1} &
\multicolumn{1}{c@{\hspace*{43pt}}}{CC} &
\multicolumn{1}{c@{\hspace*{15pt}}}{column2} &
\multicolumn{1}{l@{\hspace*{42pt}}}{column3} &
\multicolumn{2}{c@{\hspace*{42pt}}}{column4} & 
\multicolumn{2}{c}{column5} \\

% \multicolumn{2}{c@{\hspace*{12pt}}}{column5} \\
% \multicolumn{1}{l}{column3{\hspace*{10pt}}}

    &   &   &   & AA & BB & AA & BB \\

\midrule
AAAAAAAAAAAA & BBBBBBB & BBBBBBB & BBBBBBB & BBBBBBB  & BBBBBBB  & BBBBBBB  & BBBBBBB \\
AAAAAAAAAAAA & BBBBBBB & BBBBBBB & BBBBBBB & BBBBBBB  & BBBBBBB  & BBBBBBB  & BBBBBBB \\
AAAAAAAAAAAA & BBBBBBB & BBBBBBB & BBBBBBB & BBBBBBB  & BBBBBBB  & BBBBBBB  & BBBBBBB \\
\end{tabu} 
\end{myfont}
\end{table}

\end{document}
share|improve this answer
    
Thanks David for your answer. You are right, maybe we should close my other question. However, to me, it looks like that only column1 and the CC column got aligned properly. The rest of the columns are now closer to the center but still look a bit shifted to me? You said that this is a very poor technique to use but is there anything else apart from tabu and its X columns to achieve that? –  Josh Feb 8 '13 at 9:05
    
As stated in the earlier answers you can specify the full width of the table is \textwidth and then put stretch glue useing \extracolsep. I'd use one of the standard environments rather than tabu, but you can do the same with tabu if you wish. –  David Carlisle Feb 8 '13 at 9:09
    
Would you be kind enough to give me a very little example of how you would do that with the \textwidth and \extracolsep approach with which I can start off? It's always a bit tricky to find examples that fit your purposes especially for the tabu environment. People seem to only use it with the tabular* env –  Josh Feb 8 '13 at 9:17
    
why are you using tabu it has some good parts but unfortunately it is deliberately incompatible with standard latex syntax as it changes the meaning if > so I don't use it. You could use a standard tabular* here. –  David Carlisle Feb 8 '13 at 10:28
    
I tried tabular* and tabularx in the mean time and I managed to create an example using \extracolsep. However, the result of this shifted header is exactly the same. Using c columns did not help either. Getting really annoyed with it as I am trying to get this working since Monday now I went back to tabu and the X columns. I don't like how you have to specify the space between the columns as when you change the value in 1 column everything else gets shifted as well because it's baed on percentage. Whereas this does not happen with \hspace etc. You define it and the space is fixed finite –  Josh Feb 8 '13 at 11:02
show 2 more comments

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