37

I'm trying to align a specific content of a single cell. For example,

\begin{tabular}{|r|r|}
   \hline
   1 & 2 \\
   \hline
   right & to \_ the \_ left \\
   \hline
   1 & 2 \\
   \hline
\end{tabular}

Is it possible to_the_left to be aligned to the left, but to leave all others on the right? Is it even possible to align (cell contents) inside an already defined tabular environment?

Roland
  • 6,655
aksr
  • 847
  • Instead of posting code snippets, it is always best to compose a MWE that includes \documentclass and any packages, so that those trying to help don't have to recreate it. – Peter Grill Nov 02 '11 at 19:59
  • Side note: if you use an extended tabular environment with paragraph-style columns (e.g. \begin{tabularx}{15cm}{|p{3cm}|X|X|m{3cm}|}), then you can use this solution. Unfortunately, this does not seem to work with simple l, c or r columns. – Josse Apr 18 '19 at 14:27

3 Answers3

50

You can use \multicolumn{1}{l}{<content>} for to_the_left to switch the cell alignment just for this cell. If you want to have a vertical line you need to use l| instead, otherwise the line is missing for this cell.

\documentclass{article}
\begin{document}
\begin{tabular}{|r|r|}
   \hline
   111111 & 222222 \\
   \hline
   right & \multicolumn{1}{l|}{left} \\
   \hline
   1 & 2 \\
   \hline
\end{tabular}
\end{document}

Result

David Carlisle
  • 757,742
Martin Scharrer
  • 262,582
  • 2
    Yes, I already tried this solution, but it's a bit hack-ish, though. Is there more 'elegant' solution? If not, it would be nice to have better solution in upcoming LaTeX. – aksr Nov 02 '11 at 20:46
  • LaTeX3 won't be around for another while and I don't think it will handle that any better. Tables use TeX primitives to format the cells and you need some form of interface to control that. I don't think there is a simpler way than \multicolumn. You could define yourself a shorter macro for this, but that's all. – Martin Scharrer Nov 02 '11 at 20:49
  • 1
    Is there a variant of this solution that does not depend on whether the cell is adjacent to a vertical line or not? – Joachim Breitner Mar 15 '14 at 15:56
18

There is a "solution" that avoids \multicolumn{1}{l}{...} but it's really a hack:

right & left\hfill\vadjust{} \\

The \hfill alone won't do, because LaTeX works hard to remove all space from the end part of a cell. Instead of \vadjust{} also \penalty0 or \nobreak can be used.

A \multicolumn{1}{l}{...} is, probably, more visible and easier to change in case it's not needed any more.

egreg
  • 1,121,712
5

You have a natural way to set alignment for some cells with tabularray package:

\documentclass{article}
\usepackage{tabularray}
\begin{document}

\begin{tblr}{ colspec= {|r|r|}, cell{2}{2} = {l}, } \hline 111111111111 & 222222222222 \ \hline right & to-the-left \ \hline 111 & 222 \ \hline \end{tblr}

\end{document}

enter image description here

L.J.R.
  • 10,932