0

In a largetable, I want to obtain the following:

  • All cells vertically aligned to the top
  • Left column is horizontally centred, Right column is flushed left
    To be clear in the picture below:
  • Alpha should be aligned with One
  • Austria should be aligned with 1 I thought that using p instead of m should do the trick but obviously not, though there are many questions about alignment, I did not find the answer to this specific problem.

Obviously, long table, multicolumn and the small tabular are used here just to have a working example close to what is my actual document.

Obtained table with wrong vertical aligment

\documentclass[a4paper]{article}
\usepackage{longtable}
\usepackage{array}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}
\newcolumntype{L}[1]{>{\flushleft\let\newline\\\arraybackslash\hspace{0pt}}p{#1}}
\begin{document}
\begin{longtable}{| C{1cm} | C{1cm}  | L{3cm} | L{3cm} |}
\hline
\multicolumn{2}{|C{2cm}|}{
\begin{tabular}[t]{c}
    Alpha\\
    Beta\\
    Gamma\\
    Delta
\end{tabular}
}&
 \multicolumn{2}{L{6cm}|}{
 \begin{tabular}[t]{c}
    One\\
    Two
\end{tabular}
}
 \\\hline
\multicolumn{2}{|C{2cm}|}{
\begin{tabular}[t]{c}
    Austria\\
    Belgium
\end{tabular}
}&
 \multicolumn{2}{L{6cm}|}{
 \begin{tabular}[t]{c}
    1\\
    2\\
    3
\end{tabular}
}
\\\hline
\end{longtable}
\end{document}
  • flushleft is never inteneded to be used in command form it is the \begin{flushleft} environment and like center it adds vertical space (as you see) the analogue of \centering is \raggedright. also you presumably want p not m columns if you want top alignment. – David Carlisle Sep 26 '18 at 15:23
1

The problem is that you use \flushleft instead of \raggedright. \flushleft is the begin of the environment flushleft which uses the \trivlist macro (which is used internally in a lot of places in LaTeX), which happens to introduce some vertical offset.

So to solve your main issue, just use \raggedright. The following does fix some more issues and implements a \mulcol macro, which is \multicolumn with added code to account the \tabcolseps in the skipped columns and the vertical rules (adding it to the width). It assumes that next to every included column there are 2\tabcolsep of space (which is the default) plus a vertical rule. It also assumes a specific pattern in the second argument, being any tokens then a group specifying a width followed by any more tokens.

The additional issues I fixed are:

  • suppress white spaces around the columns in the sub-tabulars using @{}c@{} or similar.

  • using p type column for both L and C (this is actually required to get your vertical alignment right)

  • use full space available in the columns put together using \multicolumn (this is done with \mulcol here, in general you should keep in mind that you also get the space of the stuff in between the columns). This has no big effect on the left aligned columns (you could only see it if you used the full width for the contents), but the centred ones aren't centred compared to the other columns if you forget about this.

Complete code:

\documentclass[a4paper]{article}
\usepackage{longtable}
\usepackage{array}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash}p{#1}}
\newcolumntype{L}[1]{>{\raggedright\let\newline\\\arraybackslash}p{#1}}

\newcommand\mulcol[2]
  {%
    \mulcolA{#1}#2\endmulcolsecarg
  }
\newcommand\mulcolA{}
\long\def\mulcolA#1#2#{\mulcolB{#1}{#2}}
\newcommand\mulcolB{}
\long\def\mulcolB#1#2#3#4\endmulcolsecarg
  {%
    \multicolumn
      {#1}
      {%
        #2%
        {%
          \dimexpr
            #3%
            +#1\tabcolsep+#1\tabcolsep-2\tabcolsep
            +#1\arrayrulewidth-\arrayrulewidth
          \relax
        }%
        #4%
      }%
  }
\begin{document}
\begin{longtable}{| C{1cm} | C{1cm}  | L{3cm} | L{3cm} |}
\hline
\mulcol{2}{|C{2cm}|}{
\begin{tabular}[t]{@{}c@{}}
    Alpha\\
    Beta\\
    Gamma\\
    Delta
\end{tabular}
}&
 \mulcol{2}{L{6cm}|}{
   \begin{tabular}[t]{@{}c@{}}
    One\\
    Two
\end{tabular}
}
 \\\hline
\mulcol{2}{|C{2cm}|}{
  \begin{tabular}[t]{@{}c@{}}
    Austria\\
    Belgium
\end{tabular}
}&
 \mulcol{2}{L{6cm}|}{
 \begin{tabular}[t]{@{}c@{}}
    1\\
    2\\
    3
\end{tabular}
}
\\\hline
\end{longtable}
\end{document}

enter image description here

0
  • instead nesting tabular environments inside multicolumn cells i would rather use \makecell macro from the package of the same name (it is based on tabular environment, but for its use the code is shorter).
  • since your longtable has defined four columns i suspect that the all of them is somewhere used (but not shown in your mwe):

considering above mentioned your table can be written as follows:

\documentclass[a4paper]{article}
\usepackage{array, longtable, makecell}
\newcolumntype{C}[1]{>{\centering\arraybackslash}p{#1}}
\newcolumntype{L}[1]{>{\raggedright\arraybackslash}p{#1}}

\begin{document}
    \begin{longtable}{| C{1cm} | C{1cm}  | L{3cm} | L{3cm} |}
\hline
\multicolumn{2}{|c|}{\makecell[tc]{
    Alpha\\
    Beta\\
    Gamma\\
    Delta}}     &
\multicolumn{2}{l|}{\makecell[tl]{
    One\\
    Two}}       \\
    \hline
\multicolumn{2}{|c|}{\makecell[tc]{
    Austria\\
    Belgium}}   &
 \multicolumn{2}{l|}{\makecell[tl]{
    1\\
    2\\
    3}}         \\
\hline
1 & 2 & 3 & 4   \\
\hline
    \end{longtable}
\end{document}

which gives:

enter image description here

spurious vertical align of cells contents is removed on the same way as Skillmon do in his answer.

  • \makecell is in its core just a tabular with some code around it. Also your first column (so \multicolumn{2}{|C{2cm}|}) is not horizontally centred as you left out the two \tabcolsep and the vertical rule in between those two columns. Also in your picture the contents of the first column seem to wander rightwards. This wouldn't have happened if you used \let\newline\\ in the column definitions. – Skillmon Sep 26 '18 at 14:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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