Graphics, Figures & TablesSeveral problem with a small table

Information and discussion about graphics, figures & tables in LaTeX documents.
Post Reply
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Several problem with a small table

Post by Cham »

I'm experiencing three problems with this table, and I don't know how to fix them :

Code: Select all

\documentclass[11pt,twoside]{book}
\usepackage[T1]{fontenc}
\usepackage[total={6.25in,10in},left=1.25in,top=0.5in]{geometry}
\usepackage{amsmath}
\usepackage{graphicx}
\usepackage[dvipsnames,table]{xcolor}
\usepackage{float}
\usepackage{tabularx}

\begin{document}

\begin{table}[H]
	\centering
	\newcommand{\mystrut}{\rule[-6pt]{0pt}{20pt}}
	\begin{tabular}{l|@{}!{\mystrut}>{\centering}m{3.5em}|c|c|!{}>{}m{4em}}
	\cline{2-4}
		\rowcolor[gray]{0.95} % <--- problem here
		& \multicolumn{3}{@{}!{\mystrut}c|}{Some funny title}
		\\ \cline{2-4}
		& \multicolumn{1}{>{\centering}m{6em}|}{Word 1}
		& \multicolumn{1}{>{\centering}m{6em}|}{Word 2}
		& \multicolumn{1}{>{\centering}m{6em}|}{Word 3}
		&
		\\ \cline{1-4}
		\multicolumn{1}{|!{\mystrut}>{}m{4em}|}{Disk} & 1 & 2 & 3 & \\ \cline{1-4}
		\multicolumn{1}{|!{\mystrut}>{}m{4em}|}{Sphere} & 1 & 2 & 3 & \\ \cline{1-4}
		\multicolumn{1}{|!{\mystrut}>{}m{4em}|}{Cylinder} & 1 & 2 & 3 & \\ \cline{1-4}
	\end{tabular}
	\caption{Some description.}
\end{table}

\end{document}
Preview with issues shown in red :
table.jpg
table.jpg (28.09 KiB) Viewed 2351 times
1. I need the title to be on a gray background, but currently the gray rectangle is too large at the left side. How to remove that part?

2. The "1" in the second column aren't properly centered in their column. How to fix that?

3. I need the main part of the table to be centered on the page, so I added an empty column to the right. Something is wrong in my code, since the last vertical line isn't at the proper position. How to fix it?

Recommended reading 2024:

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

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

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Several problem with a small table

Post by Cham »

I fixed issues #2 and #3 above :

Code: Select all

\documentclass[11pt,twoside]{book}
\usepackage[total={6.25in,10in},left=1.25in,top=0.5in]{geometry}
\usepackage{graphicx}
\usepackage[dvipsnames,table]{xcolor}
\usepackage{float}
\usepackage{tabularx}

\begin{document}

\begin{table}[H]
	\centering
	\newcommand{\mystrut}{\rule[-6pt]{0pt}{20pt}}
	\begin{tabular}{l|@{}!{\mystrut}>{\centering}m{6em}|!{}>{\centering}m{6em}|!{}>{\centering}m{6em}|l}
	\cline{2-4}
		\cellcolor[gray]{0.95} % <--- problem here
		& \multicolumn{3}{@{}!{\mystrut}c|}{Some funny title}
		\\ \cline{2-4}
		& Word 1 & Word 2 & Word 3 &
		\\ \cline{1-4}
		\multicolumn{1}{|!{\mystrut}>{}m{4em}|}{Disk} & 1 & 2 & 3 & \multicolumn{1}{!{\mystrut}>{}m{4em}}{} \\ \cline{1-4}
		\multicolumn{1}{|!{\mystrut}>{}m{4em}|}{Sphere} & 1 & 2 & 3 & \\ \cline{1-4}
		\multicolumn{1}{|!{\mystrut}>{}m{4em}|}{Cylinder} & 1 & 2 & 3 & \\ \cline{1-4}
	\end{tabular}
	\caption{Some description.}
\end{table}

\end{document}
Preview :
table2.jpg
table2.jpg (23.27 KiB) Viewed 2343 times
But then I need to fix the gray background under the title. Using the command \cellcolor in the title cell correctly doesn't fix that issue, since the top horizontal bar becomes very very thin (almost invisible). I need help there! :oops:
User avatar
Cham
Posts: 937
Joined: Sat Apr 02, 2011 4:06 pm

Several problem with a small table

Post by Cham »

I removed the title and its gray background for simplicity. And I want the full table to be horizontally centered on the page. Now the problem is getting worst!
I'll never be able to understand tables in LaTeX :evil:

This code has problems :

Code: Select all

\documentclass[11pt,twoside]{book}
\usepackage[total={6.25in,10in},left=1.25in,top=0.5in]{geometry}
\usepackage{graphicx}
\usepackage{tabularx}
\begin{document}
 
\begin{table}
	\centering
	\newcommand{\mystrut}{\rule[-6pt]{0pt}{20pt}}
	\begin{tabular}{l|@{}!{\mystrut}>{\centering}m{6em}|!{}>{\centering}m{6em}|!{}>{\centering}m{6em}|}
	\cline{2-4}
		& Word 1 & Word 2 & Word 3
		\\ \cline{1-4}
		\multicolumn{1}{|!{\mystrut}>{}m{4em}|}{Disk} & 1 & 2 & 3
		\\ \cline{1-4}
		\multicolumn{1}{|!{\mystrut}>{}m{4em}|}{Sphere} & 1 & 2 & 3
		\\ \cline{1-4}
		\multicolumn{1}{|!{\mystrut}>{}m{4em}|}{Cylinder} & 1 & 2 & 3
		\\ \cline{1-4}
	\end{tabular}
	\caption{Some description.}
\end{table}
 
\end{document}
What is wrong here? I want all items to be centered in its cell (vertically and horizontally). I need all the columns to have the same width, except the first one. What am I doing wrong?
Post Reply