1

I am facing the problem to format my tabularx in a proper way. The tables should have the following format with centerized text:

wanted

My code for that:

\begin{table}[H]
    \begin{tabularx}{\linewidth}{X|p{3cm}|p{3cm}}
        \textbf{Text} & \textbf{First Number (that is a longer text)} & \textbf{Second number (indeed also a long text)}\\
        \hline \\[-0.5ex]
        This is a long text with no sense but that might contain a linebreak & 3 & 3\\[1.5ex]
    \end{tabularx}
    \caption{mytable}
\end{table}

So I have been trying around a little bit, but it turns out that formatting is harder for me than I thought earlier. When centering the text the tabularx seems to have no floating column width. How do I do that?

The closest I got to that was the folling code:

\begin{table}[H]
    \begin{tabularx}{\linewidth}{c|X|X}
        \textbf{Text} & \textbf{First Number (that is a longer text)} & \textbf{Second number (indeed also a long text)}\\
        \hline \\[-0.5ex]
        This is a long text with no sense but that might contain a linebreak & 3 & 3\\[1.5ex]
    \end{tabularx}
    \caption{mytable - v2}
\end{table}

\begin{table}[H]
    \begin{tabularx}{\linewidth}{X|X|X}
        \textbf{Text} & \textbf{First Number (that is a longer text)} & \textbf{Second number (indeed also a long text)}\\
        \hline \\[-0.5ex]
        This is a long text with no sense but that might contain a linebreak & 3 & 3\\[1.5ex]
    \end{tabularx}
    \caption{mytable - v3}
\end{table}

which turns out as:

enter image description here

  • The text in each cell should be centred vertically or horizontally? – Bernard Oct 16 '18 at 9:53
  • I suppose it would look best if it was centred vertically and horizontally – Rüdiger Oct 16 '18 at 10:07
  • For column heads, yes (usually). For long texts in cells, this is debatable. – Bernard Oct 16 '18 at 10:16
  • Lets say we want it horizontally and vertically – Rüdiger Oct 16 '18 at 10:16
1

Is it something like this you want?

\documentclass[12pt,a4paper,twoside,openright]{book}
\usepackage{geometry}
\usepackage{ tabularx, caption, float}
\renewcommand{\tabularxcolumn}[1]{>{\centering\arraybackslash}m{#1}}

\begin{document}

\begin{table}[H]
\setlength{\extrarowheight}{2pt}
    \centering
    \begin{tabularx}{\linewidth}{>{\hsize=1.5\hsize}X|>{\hsize=0.75\hsize}X|>{\hsize=0.75\hsize}X}
        \textbf{Text} & \textbf{First Number (that is a longer text)} & \textbf{Second number (indeed also a long text)} \\
        \hline
        This is a long text with no sense but that might contain one or several linebreaks & 3 & 3 %\\
    \end{tabularx}
    \caption{mytable}
\end{table}

\end{document} 

enter image description here

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.