Graphics, Figures & Tablestabularx | Adjusted Column Width

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
NinaKeul
Posts: 5
Joined: Fri Sep 07, 2012 9:48 pm

tabularx | Adjusted Column Width

Post by NinaKeul »

Hi all,

After my first steps in LaTeX, I am now trying to create a table. It will have 9 columns, where the first column needs to be more wide, since it contains text. whereas the other eight will contain just numbers an can be of equal width. However, I do not seem to be able to do that even after 1.5 hours of trying and searching this forum... :lol: laughing at myself...

Please find below a minimum working example of my table (I know \linewidth is fixed in this example, but that was the only way I could produce an example to post).

Code: Select all

Code, edit and compile here:
\documentclass[12pt]{article}
\usepackage{booktabs}
\usepackage{tabularx}
\begin{document}
\begin{table}[!ht]
\begin{tabularx}{\linewidth}{XXXXXXXXX}\toprule
Treatments \\ \cmidrule(l){2-9}
& A1 & A2 & A3 & A4 & B1 & B2 & B3 & B4 \\ \midrule
this is a longer text& 180 & 380 & 950 & 1400 & 180 & 380 & 950 & 1400 \\
this is a longer text& 180 & 380 & 950 & 1400 & 180 & 380 & 950 & 1400 \\
this is a longer text& 180 & 380 & 950 & 1400 & 180 & 380 & 950 & 1400 \\ \bottomrule
\end{tabularx}
\end{table}
\end{document}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
I also have problems with orientation: The very first row should be centered ("treatments"), the first column should be left aligned and the other columns should be centered.

Thank you so much in advance,
Nina

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

josephwright
Site Moderator
Posts: 814
Joined: Tue Jul 01, 2008 2:19 pm

Re: tabularx | Adjusted Column Width

Post by josephwright »

Is there a reason you are not using a standard tabular environment? That would seem to be a lot easier for the table you posted.
Joseph Wright
NinaKeul
Posts: 5
Joined: Fri Sep 07, 2012 9:48 pm

Re: tabularx | Adjusted Column Width

Post by NinaKeul »

Hi Joseph,

what exactly do you mean by a standard tabular environment?

Best,
Nina
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

tabularx | Adjusted Column Width

Post by localghost »

NinaKeul wrote:[…] However, I do not seem to be able to do that even after 1.5 hours of trying and searching this forum. […]
That is very astonishing since this is discussed very often (not only here).
NinaKeul wrote:[…] what exactly do you mean by a standard tabular environment? […]
What Joseph means is the standard {tabular} (← Link!) environment which does not support the X column type.

From your descriptions I think that you rather want something like this.

Code: Select all

Code, edit and compile here:
\documentclass[12pt]{article}
\usepackage{booktabs}
\usepackage{tabularx}
\begin{document}
\begin{table}[!ht]
\begin{tabularx}{\linewidth}{X*{9}{c}}\toprule
& \multicolumn{8}{c}{Treatments} \\ \cmidrule(lr){2-9}
& A1 & A2 & A3 & A4 & B1 & B2 & B3 & B4 \\ \midrule
The quick brown jumps over the lazy dog & 180 & 380 & 950 & 1400 & 180 & 380 & 950 & 1400 \\ \addlinespace
The quick brown jumps over the lazy dog & 180 & 380 & 950 & 1400 & 180 & 380 & 950 & 1400 \\ \addlinespace
The quick brown jumps over the lazy dog & 180 & 380 & 950 & 1400 & 180 & 380 & 950 & 1400 \\ \bottomrule
\end{tabularx}
\end{table}
\end{document}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Thorsten
Attachments
The resulting output.
The resulting output.
tabularx-adjusted-column.png (23.99 KiB) Viewed 49181 times
NinaKeul
Posts: 5
Joined: Fri Sep 07, 2012 9:48 pm

Re: tabularx | Adjusted Column Width

Post by NinaKeul »

Hi Thorsten,

thanks for the solution, it works just fine.

I had many similar solutions tried out but not exactly that combination.

Have a nice evening,
Nina
KSD
Posts: 1
Joined: Fri Oct 13, 2023 1:18 pm

tabularx | Adjusted Column Width

Post by KSD »

localghost wrote:
From your descriptions I think that you rather want something like this.

Code: Select all

Code, edit and compile here:
\documentclass[12pt]{article}
\usepackage{booktabs}
\usepackage{tabularx}
\begin{document}
\begin{table}[!ht]
\begin{tabularx}{\linewidth}{X*{9}{c}}\toprule
& \multicolumn{8}{c}{Treatments} \\ \cmidrule(lr){2-9}
& A1 & A2 & A3 & A4 & B1 & B2 & B3 & B4 \\ \midrule
The quick brown jumps over the lazy dog & 180 & 380 & 950 & 1400 & 180 & 380 & 950 & 1400 \\ \addlinespace
The quick brown jumps over the lazy dog & 180 & 380 & 950 & 1400 & 180 & 380 & 950 & 1400 \\ \addlinespace
The quick brown jumps over the lazy dog & 180 & 380 & 950 & 1400 & 180 & 380 & 950 & 1400 \\ \bottomrule
\end{tabularx}
\end{table}
\end{document}
הההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההההה
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Thorsten
Great, Thorsten! Also worked for me.
Can you explain for a newbie how X*{9}{c} works? It seems that this made all the job. In the original code it was XXXXXXXXX
Post Reply