0

I want a table with two columns; the left column should be left-justified, the right right-justified, but with possible manual line breaks in either column. I am using a tabularx environment \begin{tabularx}{\textwidth}{X|X} which I believe should automatically calculate the column size to be exactly half the textwidth value. To right justify the second column, I try the following:

\begin{tabularx}{\textwidth}{| X | >{\raggedleft\arraybackslash}X |}
\hline
Left line 1 (which could be quite long) \newline
Left line 2 \newline
Left line 3
& 
Right line 1 (which could also be quite long) \newline
Right line 2 \newline
Right line 3 \newline
Right line 4
\tabularnewline
\hline
\end{tabularx}

Here I end the table after just one row, but in principle I may want more similar such rows.

When I compile, the right column is not right aligned - only the last "Right line 4" is:

Output

If I do not put the \newlines in then the right column is right justified as I expect (but as a single paragraph without manual linebreaks). The \newline seems to clash somewhere with the alignment, and I can't figure out how.

I notice that if I use \linebreak instead of \newline my problem goes away. Is this a correct usage of linebreak though?

Can someone explain why the newlines don't work as I expect? And what is the best solution? Many thanks in advance.

2 Answers 2

2

My advice is to use the package ragged2e, which gives better definitions for \raggedright and \raggedleft (and \centering) under the capitalized names \RaggedRight, \RaggedLeft and \Centering (or with an option under the original names). These commands are better suited for use in narrow columns like in tabulars. It also solves the \newline problem when these commands are active.

\documentclass{article}

\usepackage{ragged2e}
\usepackage{tabularx}

\begin{document}
\begin{tabularx}{\textwidth}{| X | >{\RaggedLeft\arraybackslash}X |}
\hline
Left line 1 (which could be quite long) \newline
Left line 2 \newline
Left line 3
& 
Right line 1 (which could also be quite long) \newline
Right line 2 \newline
Right line 3 \newline
Right line 4
\tabularnewline
\hline
\end{tabularx}
\end{document}

enter image description here

3
  • This is great, I knew of ragged2e but didn't know \newline behaved differently. Commented Mar 27 at 13:17
  • One question though: I get underfilled \hbox warnings when I copy your code. They seem totally harmless, but do you know why they are there, and how one can get rid of them? Commented Mar 27 at 13:19
  • See tex.stackexchange.com/a/18019/113546 Commented Mar 27 at 16:56
0

After a bit more digging, the answer to what is going on here is essentially contained within this answer. To recapitulate, the raggedleft (equivalent to the flushright environment) command is adding an \hfil to the start of each line. As far as I can tell, \newline = \hfil\break, so that if I write this:

\begin{tabularx}{\textwidth}{| X | >{\raggedleft\arraybackslash}X |}
    \hline
    Left line 1 (which could be quite long) \newline
    Left line 2 \newline
    Left line 3
    & 
    Right line 1 (which could also be quite long \lipsum[66])\newline
    Right line 2 \newline
    Right line 3 \newline
    Right line 4
    \tabularnewline
    \hline
\end{tabularx}

I get the equivalent of:

\begin{tabularx}{\textwidth}{| X | >{\raggedleft\arraybackslash}X |}
    \hline
    Left line 1 (which could be quite long)\hfil\break
    Left line 2\hfil\break
    Left line 3
    & 
    \hfil Right line 1 (which could also be quite long\break
    \hfil Nunc sed pede. Praesent vitae lectus.\break
    \hfil Praesent neque justo, vehicula eget,\break
    \hfil interdum id, facilisis et, nibh. Phasellus at\break
    \hfil purus et libero lacinia dictum. Fusce\break
    \hfil aliquet. Nulla eu ante placerat leo semper\break
    \hfil dictum. Mauris metus. Curabitur lobortis.\break
    \hfil Curabitur sollicitudin hendrerit nunc.\break
    \hfil Donec ultrices lacus id ipsum.)\break\hfil\break
    \hfil Right line 2\hfil\break
    \hfil Right line 3\hfil\break
    \hfil Right line 4
    \tabularnewline
    \hline
\end{tabularx}

and so we can see that the last line before each \newline in the raggedleft environment will be centred, explaining why it looks like this: Output

This is actually independent of the tabular environment - it is purely to do with flushright environments and the \newline command.

We can see from the above that it should be perfectly safe to use a \linebreak command instead, since each line's horizontal space is already being filled due to the raggedleft environmment. Alternatively, one could use plain \break, or the redundant \hfil\linebreak (but not \hfil\break!).

Ps: it would be much appreciated if an experienced person could comment on my answer to see if I have understood everything correctly. Also if they could comment on the difference between the \break, \linebreak and \newline. Indeed, I can't find the actual definition of \newline in documentation anywhere, so if someone could comment on that that would also be great. Otherwise I will ask some of these as a new question.

You must log in to answer this question.

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