2

I have encountered a problem using \centering within the tabular environment. The following code

\documentclass{article}

\begin{document}

\begin{tabular}{|r|r|} \hline
\centering{Lolcats}  & \centering{.com} \\ \hline
\end{tabular}

\end{document}

generates the error "LaTeX Error: Something's wrong--perhaps a missing \item." The error leaves by removing "\\ \hline", but this also removes the last line of the table. Any ideas on what has gone wrong? Thanks!

5
  • What is \centering supposed to do? The column already has center alignment.
    – egreg
    Jul 18, 2013 at 13:31
  • Sorry, I have now replaced c|c with r|r. I have greatly simplified my original table, in which the \centering command does have an effect.
    – Doubt
    Jul 18, 2013 at 13:42
  • You replaced the same type of command with another, as r,l,c all make a box that is just wide enough for the cell to fit in. Jul 18, 2013 at 13:47
  • If you want to override the alignment for a single cell, use \multicolumn{1}{c}{Lolcats} (add also the rules, if you really want ruled tables). Please, make a more significant example.
    – egreg
    Jul 18, 2013 at 13:47
  • The original table is such that additional rows make the entries of the table wider than necessary. I am seeking to have some rows right justified, while a select few centered. More exactly, though, I am wondering simply what is causing the error above.
    – Doubt
    Jul 18, 2013 at 13:50

1 Answer 1

2

Probably you also want to center the cells vertically. LaTeX tabulars have a different spacing model as HTML. Invisible struts help to keep a uniform line spacing. Via struts, the \baselineskip space is divided in 70% above and 30% below the base line. Since your text does not contain glyphs with descenders, the space below the base line looks too large in comparison to the space above the text.

The following example shows some workarounds. For simplicity the vertical margins will use the same as the horizontal: \tabcolsep.

By redefining

\renewcommand*{\arraystretch}{0}

the effect of the struts are nullified. The margin below can be added via the optional argument of \\. The margin above can be created by increasing the height of the row. \raisebox{\tabcolsep}{\vphantom{L}} applied to the highest letter L creates this invisible artificial height.

The next example uses an additional row instead with the height of the margin.

The example file concludes with an example, how this can be done with \fbox. \kern\fboxrule prevents the double line width of the touching \fboxes.

Package array moves the vertical lines outside the cells. The effect is visible in comparison with the \fbox example. With package array the widths are the same. Otherwise, the tabular examples have smaller widths.

\documentclass{article}
\usepackage{array}

\begin{document}

\begin{tabular}{|c|c|} \hline
  Lolcats & .com \\ \hline
\end{tabular}

\begingroup
  \renewcommand*{\arraystretch}{0}%
  \begin{tabular}{|c|c|} \hline
    \raisebox{\tabcolsep}{\vphantom{L}}%
    Lolcats  & .com \\[\tabcolsep]
    \hline
  \end{tabular}
\endgroup

\begingroup
  \renewcommand*{\arraystretch}{0}%
  \begin{tabular}{|c|c|} \hline
    \rule{0pt}{\tabcolsep} & \\
    Lolcats  & .com \\[\tabcolsep]
    \hline
  \end{tabular}
\endgroup

\setlength{\fboxsep}{\tabcolsep}
\fbox{Lolcats}\kern-\fboxrule\fbox{\vphantom{L}.com}

\end{document}

enter image description here

You must log in to answer this question.

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