5

I would like to have centered cells and have manual linebreaks within tabularx. I searched a lot in stackexchange and other latex related pages, but cannot find a solution

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{tabularx}
\usepackage[]{multirow}
\usepackage[]{MnSymbol}
\usepackage[]{array}
\usepackage{multicol}

\begin{document}

\maketitle

\section{Introduction} \newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1}} \begin{table} \centering \begin{tabularx}{1\linewidth}{lX|P{0.2\linewidth}|P{0.2\linewidth}|P{0.2\linewidth}} & & \textbf{firmware} & \textbf{bootloader} & \textbf{os} \ \multicolumn{2}{l}{\textbf{Target1}} & & & \ & {\footnotesize rot\par} & test123 \newline hallo & {\footnotesize $(\uparrow)$ \newline testtext\par} & {\footnotesize $(\nearrow)$ \newline hardware\par} \ \hline \hline \end{tabularx} \end{table}

\end{document}

Why is the first line of each cell not center aligned? If I have no linebreak within the cell the first line is correctly aligned. How to solve this? Thanks in advance

Edit: I compiled the code with overleaf and some other online latex environments to ensure my environment is not fishy enter image description here

3 Answers3

6

You asked,

Why is the first line of each cell not center aligned? ... How to solve this?

I'm afraid I cannot tell you why the problem you've encountered arises, but I can propose a solution: Load the ragged2e package, which provides a macro called \Centering, and replace

>{\centering\arraybackslash}p{#1}

with

>{\Centering}p{#1}

enter image description here

\documentclass{article}

\usepackage{tabularx,ragged2e} %% 2 ways to create a centered "p"-type column: \newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1}} \newcolumntype{Q}[1]{>{\Centering}p{#1}}

\usepackage[skip=0.333\baselineskip]{caption} % optional

\begin{document}

\begin{table}[ht] \caption{with \ttfamily >{\string\centering\string\arraybackslash}} \begin{tabularx}{\linewidth}{ X {3}{| P{0.2\linewidth}} } & \textbf{firmware} & \textbf{bootloader} & \textbf{os} \ \hline Target1 & test123 \newline hallo & \footnotesize $(\uparrow)$ \newline testtext & \footnotesize $(\nearrow)$ \newline hardware \ \hline \end{tabularx} \end{table}

\begin{table}[h] \caption{with \ttfamily >{\string\Centering}} \begin{tabularx}{\linewidth}{ X {3}{| Q{0.2\linewidth}} } & \textbf{firmware} & \textbf{bootloader} & \textbf{os} \ \hline Target1 & test123 \newline hallo & \footnotesize $(\uparrow)$ \newline testtext & \footnotesize $(\nearrow)$ \newline hardware \ \hline \end{tabularx} \end{table}

\end{document}

Mico
  • 506,678
5

The reason why this does not work as expected is rooted in the fact that \centering only works correctly if the relevant text ends with a \par.

\documentclass[border=10pt]{standalone}  
\begin{document}

\parbox{5cm}{% \centering This is a line \newline And this is another line % }

\parbox{5cm}{% \centering This is a line \par And this is another line % (implicit \par at the end of the \parbox) }

\end{document}

enter image description here

The same is true if you use \centering inside a p column, since this essentially places a \parbox inside the table cell. So instead of using \newline inside these cells, you should use \par (or enter a blank line):

\documentclass{article}
\usepackage{tabularx}
\newcolumntype{P}[1]{>{\centering\arraybackslash}p{#1}}

\begin{document}

\begin{table} \centering \begin{tabularx}{1\linewidth}{lX|P{0.2\linewidth}|P{0.2\linewidth}|P{0.2\linewidth}} & & \textbf{firmware} & \textbf{bootloader} & \textbf{os} \ \multicolumn{2}{l}{\textbf{Target1}} & & & \ & \footnotesize rot & test123 \par hallo & \footnotesize $(\uparrow)$ \par testtext & \footnotesize $(\nearrow)$ \par hardware \ \hline \hline \end{tabularx} \end{table}

\end{document}

enter image description here

I removed all packages from the code that are not necessary for this example and simplified the code a bit.

1

I had no centring problem with your code, but I propose a simpilfication of your code if you have a recent version of array, which defines a wc{some length} column type. Also needless to load \usepackage[utf8]{inputenc} nowadays, since this is the encoding that latex expects by default now.

    \documentclass{article}
    \usepackage{tabularx}
    \usepackage[]{multirow}
    \usepackage[]{MnSymbol}
    \usepackage[]{array}
    \usepackage{multicol}
\begin{document}

%\maketitle

\section{Introduction}

\begin{table}
    \centering
    \begin{tabularx}{1\linewidth}{l>{\centering\arraybackslash}X|wc{0.2\linewidth}|wc{0.2\linewidth}|wc{0.2\linewidth}|}
        & & \textbf{firmware} & \textbf{bootloader} & \textbf{os} \\
        \multicolumn{2}{l|}{\textbf{Target1}} & & & \\
        & {\footnotesize rot\par} & test123 \newline hallo & {\footnotesize $(\uparrow)$ \newline testtext\par} & {\footnotesize $(\nearrow)$ \newline hardware\par} \\
        \hline
        \hline
    \end{tabularx}
\end{table}

\end{document} 

enter image description here

Bernard
  • 271,350
  • 2
    As far as I understood the question, the problem was regarding the alignment when a \newline is placed. Your code seems to ignore this. – Jasper Habicht Aug 04 '22 at 12:30
  • 1
    @JasperHabicht: I just copy-pasted the code in the question, and modified what I mentioned to simplify the code`. But you're right,I'll see whether \newline works. with this code – Bernard Aug 04 '22 at 12:39