4

Here's an example:

\documentclass{article}
\usepackage[papersize={4.8 in, 6.4 in}, top=0.10cm, bottom=0.5cm, right=0.2cm, left=0.2cm]{geometry}
\begin{document}
\begin{center}
\scriptsize
\begin{tabular}{|l|l|}
\multicolumn{2}{c}{\enspace}                                                                       \\
left-aligned text in column 1                & left-aligned text in column 2                       \\
left-aligned text in column 1                & left-aligned text in column 2                       \\
\multicolumn{2}{c}{\enspace}                                                                       \\
\multicolumn{2}{l}{\hspace{20pt} short line (left-aligned centered block)}                         \\
\multicolumn{2}{l}{\hspace{20pt} now a longer (line in the same left-aligned centered block)}      \\
\multicolumn{2}{c}{\enspace}                                                                       \\
\multicolumn{2}{c}{short line (centered, not left-aligned)}                                        \\
\multicolumn{2}{c}{this is a longer line (centered, not left-aligned)}                             \\
\multicolumn{2}{c}{\enspace}                                                                       \\
left-aligned text in column 1                & left-aligned text in column 2                       \\
left-aligned text in column 1                & left-aligned text in column 2                       \\
\end{tabular}
\end{center}

\end{document}

I have a two-column table and I need to center text that spans the two columns while staying left-aligned.

In the above code I use a left-aligned multicolumn and \hspace to arbitrarily indent the block by 20pt.

Is there any way I can have the compiler compute the width/length of the indentation?

A suitable indentation for the block to be centered should be something like: (width of the table - average line length) / 2.

What would be the proper way to do this?

Update:

I went for the nested tabular and got pretty much the same layout as the original:

enter image description here

  • If you have several such \multicolumns, do they have to be left-aligned w.r.t. each other? – Bernard Aug 23 at 19:55
  • use NiceTabular with the option block – js bibra Aug 24 at 2:13
  • @Bernard in the original document the French printer typesetter didn't do it. My last shot is pretty close to what he came up with. I ususally try to do this outside table when, e.g. the text is interspersed with bits of verse or songs… provided the lines are the same length in case two such end up on the same page. I feel it looks better when they are aligned with regards to each other. – T F Aug 24 at 18:53
  • @jsbibra would that be package nicematrix? I don't need yet another package at this point but after a cursorily look at the documentation it looks like it may come in handy if I need to do something more complex at some point. – T F Aug 24 at 18:58
  • You can obtain his result automatically withthe eqparbox` package: among others, the \eqparbox{tag}{…} command makes all eqparboxes sharing the same tag have the width of the largest natural width of each content. See the documentation, for more details. – Bernard Aug 24 at 19:01
5

With stackengine, and its \Centerstack command, which can take an optional argument for the alignment (default is c), and \addlinespace(from booktabs) to simplify the vertical spacing around these blocks:

\documentclass{article}
\usepackage[papersize={4.8 in, 6.4 in}, top=0.10cm, bottom=0.5cm, hmargin=0.2cm]{geometry}
\usepackage{booktabs}
\usepackage[usestackEOL]{stackengine}

\begin{document}

\begin{center}
\scriptsize\setlength{\defaultaddspace}{2.7ex}
\begin{tabular}{|l|l|}
\multicolumn{2}{c}{\enspace} \\
left-aligned text in column 1 & left-aligned text in column 2 \\
left-aligned text in column 1 & left-aligned text in column 2 \\
\addlinespace
\multicolumn{2}{c}{\Centerstack[l]{short line (left-aligned centered block) \\
now a longer (line in the same left-aligned centered block)}} \\
\addlinespace
\multicolumn{2}{c}{\Centerstack{short line (centered, not left-aligned) \\
this is a longer line (centered, not left-aligned)}} \\
\addlinespace
left-aligned text in column 1 & left-aligned text in column 2 \\
left-aligned text in column 1 & left-aligned text in column 2 \\
\end{tabular}
\end{center}

\end{document}

enter image description here

Edit: loading eqparbox, you can obtain this alignment withe the code below:

\begin{center}
\scriptsize\setlength{\defaultaddspace}{2.7ex}
\begin{tabular}{|l|l|}
\multicolumn{2}{c}{\enspace} \\
left-aligned text in column 1 & left-aligned text in column 2 \\
left-aligned text in column 1 & left-aligned text in column 2 \\
\addlinespace
\multicolumn{2}{c}{\eqparbox{LC}{\Centerstack[l]{short line (left-aligned centered block) \\
now a longer (line in the same left-aligned centered block)}}} \\
\addlinespace
\multicolumn{2}{c}{\eqparbox{LC}{\Centerstack[l]{short line (centered, not left-aligned) \\
this is a longer line (centered, not left-aligned)}}} \\
\addlinespace
left-aligned text in column 1 & left-aligned text in column 2 \\
left-aligned text in column 1 & left-aligned text in column 2 \\
\end{tabular}
\end{center}

enter image description here

| improve this answer | |
  • Thank you very much. You can delete your previous comment. – Sebastiano Aug 24 at 20:59
4

With the following two little tricks you can get two lines that are left aligned with respect to each other, but centered within the table.

In the first example, I have used a single, centered p type column for both lines of text, while the second example is based on a nested left aligned tabular inside of a centered multicolumn.

enter image description here

\documentclass{article}
\usepackage{array}
\usepackage{makecell}
\usepackage[papersize={4.8 in, 6.4 in}, top=0.10cm, bottom=0.5cm, right=0.2cm, left=0.2cm]{geometry}
\begin{document}
\begin{center}
\scriptsize
\begin{tabular}{|l|l|}
\multicolumn{2}{c}{\enspace}                                                                             \\
left-aligned text in column 1   & left-aligned text in column 2                                          \\
left-aligned text in column 1   & left-aligned text in column 2                                          \\
\multicolumn{2}{c}{}                                                                                     \\
\multicolumn{2}{>{\centering\arraybackslash}p{8cm}}{short line (centered, not left-aligned) 
                                                    \newline 
                                                    this is a longer line (centered, not left-aligned)}  \\
\multicolumn{2}{c}{}                                                                                     \\
left-aligned text in column 1   & left-aligned text in column 2                                          \\
left-aligned text in column 1   & left-aligned text in column 2                                          \\
\multicolumn{2}{c}{}                                                                                     \\
\multicolumn{2}{c}{\begin{tabular}{@{}l@{}} 
                     short line (centered, not left-aligned) \\ 
                     this is a longer line (centered, not left-aligned)
                   \end{tabular}}                                                                        \\
\multicolumn{2}{c}{}                                                                                     \\
left-aligned text in column 1   & left-aligned text in column 2                                          \\
left-aligned text in column 1   & left-aligned text in column 2                                          \\
\end{tabular}
\end{center}

\end{document}
| improve this answer | |
4

By using \makecell and \mbox your table can be written also on the following way:

\documentclass{article}
\usepackage[papersize={4.8 in, 6.4 in}, 
            top=0.10cm, bottom=0.5cm, right=0.2cm, left=0.2cm]{geometry}
\usepackage{array, makecell}
\usepackage{xparse}
\NewExpandableDocumentCommand\mcc{O{2}m}
    {\multicolumn{#1}{c}{#2}}

\begin{document}
    \begin{center}
\scriptsize
\begin{tabular}{|l|l|}
\mcc{}                                                          \\
left-aligned text in column 1   & left-aligned text in column 2 \\
left-aligned text in column 1   & left-aligned text in column 2 \\
\mcc{}                                                          \\
\mcc{\mbox{%
     \makecell[l]{short line (centered, not left-aligned)\\
                  this is a longer line 
                  (centered, not left-aligned)}}
     }                                                          \\
\mcc{}                                                          \\
left-aligned text in column 1   & left-aligned text in column 2 \\
left-aligned text in column 1   & left-aligned text in column 2 \\
\mcc{}                                                          \\
\mcc{short line (centered, not left-aligned)}                   \\
\mcc{this is a longer line (centered, not left-aligned)}        \\
\mcc{}                                                          \\
left-aligned text in column 1   & left-aligned text in column 2 \\
left-aligned text in column 1   & left-aligned text in column 2 \\
\end{tabular}
    \end{center}
\end{document}

enter image description here

| improve this answer | |

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.