34

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?

  • 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 2 '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 at 14:27
42

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

  • 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 2 '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 2 '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
16

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.

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.