0

I want to make a outlined table with some centered text (and images, but not shown in the example). This is an example code:

    \begin{tabular}{|p{16cm}|}
      \hline 
      {\centering{my text}\par}\\\hline
    \end{tabular}

This works, but adds an empty row at the end of the table. I would like to avoid having this row.

Does anyone knows hot to do this.

thanks in advance

5
  • Welcome to TeX SX! Remove \par, probably.
    – Bernard
    Commented Aug 14, 2019 at 18:29
  • @Bernard if he just removes the \par, the \centering won't have an effect.
    – Skillmon
    Commented Aug 14, 2019 at 18:31
  • He just has to add it to the table preamble.
    – Bernard
    Commented Aug 14, 2019 at 18:33
  • @Bernard see my answer.
    – Skillmon
    Commented Aug 14, 2019 at 18:33
  • Oh! yes. I'm sorry, I hadn't seen it.
    – Bernard
    Commented Aug 14, 2019 at 18:36

2 Answers 2

1

You should load the array package and use its facilities to centre a fixed-width column:

\documentclass[]{article}

\usepackage{array}

\begin{document}
\begin{tabular}{|>{\centering\arraybackslash}p{16cm}|}
\hline
my text \\
\hline
\end{tabular}
\end{document}
1
  • Works like a charm!
    – javier
    Commented Aug 14, 2019 at 18:43
2

The problem is that \centering redefines \\. Use \tabularnewline instead:

\documentclass{article}

\begin{document}

\begin{tabular}{|p{10cm}|}
\hline 
{\centering{my text}\par}\\\hline
\end{tabular}

\bigskip

\begin{tabular}{|p{10cm}|}
\hline 
\centering my text\tabularnewline
\hline
\end{tabular}

\end{document}

enter image description here

On the other hand, if p is used just for single line fixed width cells, use the w column type.

\documentclass{article}
\usepackage{array}

\begin{document}

\begin{tabular}{|w{c}{10cm}|}
\hline
my text \\
\hline
\end{tabular}

\end{document}

enter image description here

1
  • Thank you very much! That was a lot of useful info!
    – javier
    Commented Aug 15, 2019 at 15:14

You must log in to answer this question.

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