3

I have this table, where the total width of the two columns (a) and (b) can vary depending on the text width of Figure X above it. How can I get (a) and (b) to each have the same width always? Also, I circled in red a problem where the vertical lines are not aligned?

enter image description here

\documentclass[12pt,a4paper]{article}
\usepackage{graphicx}
\usepackage[table,xcdraw]{xcolor}
\renewcommand{\arraystretch}{1.5} 
\begin{document}

\begin{figure}
    \centering
    \includegraphics{example-image-a}
    \caption{Caption}
    \label{fig:my_label}
\end{figure}

\begin{table}
\centering
\begin{tabular}{rc|cc}
\multicolumn{1}{l}{} & \multicolumn{1}{l}{} & \multicolumn{2}{|c}{Figure~\ref{fig:my_label} sub-plot}                                    \\ \hline
Variable             & Unit                 & (a)                          & (b)                          \\ \hline
One          & m             & \cellcolor[HTML]{FFCCC9}Vary & 50                           \\
Two              & kg                    & 0.5                          & \cellcolor[HTML]{FFCCC9}Vary \\
Three           & Hz                   & 1000                        & 1000                        
\end{tabular}
\end{table}

\end{document}
  • I should add, the text in the red cells which says "Vary", that has a different meaning. It is not related to anything to do with the cell width. – Johnny J. L. Jul 23 '18 at 15:31
  • For the problem of alignment make sure you zoom it 800% because usually is a little visual glitch. – manooooh Jul 23 '18 at 15:32
6

You need to compute the width of the header, because when the total natural size of spanned columns doesn't fit the multicolumn entry, the excess space goes in the last spanned column.

Do this only if you find that there is a mismatch: otherwise you'll get wrong size.

\documentclass[12pt,a4paper]{article}
\usepackage[table,xcdraw]{xcolor}
\usepackage{array}

\renewcommand{\arraystretch}{1.5} 

\newlength{\templen}

\begin{document}

\begin{figure}[htp]
\caption{Just for the number}\label{fig:my_label}
\end{figure}

\begin{table}[htp]
\centering

\settowidth{\templen}{Figure~\ref{fig:my_label} sub-plot}
\setlength{\templen}{\dimexpr(\templen-2\tabcolsep)/2}

\begin{tabular}{ r c | *{2}{>{\centering\arraybackslash}p{\templen}} }
 & & \multicolumn{2}{c}{Figure~\ref{fig:my_label} sub-plot}                   \\
\hline
Variable & Unit & (a)                          & (b)                          \\
\hline
One      & m    & \cellcolor[HTML]{FFCCC9}Vary & 50                           \\
Two      & kg   & 0.5                          & \cellcolor[HTML]{FFCCC9}Vary \\
Three    & Hz   & 1000                         & 1000                        
\end{tabular}
\end{table}

\end{document}

Use \ref* in \settowidth in case you're loading hyperref.

enter image description here

You don't need \multicolumn for empty cells. The misalignment was due to the fact that the rule has to be specified in the preceding column (exception: a starting rule).

Here's how I'd typeset the table.

\documentclass[12pt,a4paper]{article}
\usepackage[table,xcdraw]{xcolor}
\usepackage{booktabs}

\begin{document}

\begin{figure}[htp]
\caption{Just for the number}\label{fig:my_label}
\end{figure}

\begin{table}[htp]
\centering

\begin{tabular}{ @{} l c c c @{} }
\toprule
\multicolumn{4}{@{}c@{}}{Figure~\ref{fig:my_label} sub-plot} \\
\midrule
Variable & Unit & (a)                          & (b)                          \\
\midrule
One      & m    & \cellcolor[HTML]{FFCCC9}Vary & 50                           \\
Two      & kg   & 0.5                          & \cellcolor[HTML]{FFCCC9}Vary \\
Three    & Hz   & 1000                         & 1000                         \\
\bottomrule
\end{tabular}
\end{table}

\end{document}

But probably the text in the first row should go in the caption to the table.

enter image description here

By the way, you don't need the figure and table go in different floats: a tabular can very well go in a figure environment. Something like the following might do.

\documentclass[12pt,a4paper]{article}
\usepackage[table,xcdraw]{xcolor}
\usepackage{booktabs}
\usepackage{graphicx}

\begin{document}

\begin{figure}[htp]
\centering

\hspace*{\fill}%
\includegraphics[width=0.5\textwidth]{example-image-duck}%
\hspace*{\fill}%
\begin{tabular}[b]{ @{} l c c c @{} }
\toprule
Variable & Unit & (a)                          & (b)                          \\
\midrule
One      & m    & \cellcolor[HTML]{FFCCC9}Vary & 50                           \\
Two      & kg   & 0.5                          & \cellcolor[HTML]{FFCCC9}Vary \\
Three    & Hz   & 1000                         & 1000                         \\
\bottomrule
\end{tabular}%
\hspace*{\fill}

\caption{This is the image of a duck, with data next to it.}\label{fig:my_label}

\end{figure}

\end{document}

enter image description here

4

To have the last two columns occupy the same width automatically, use the X column specification from tabularx for them. To further center their content, just modify X by adding >{\centering\arraybackslash}X.

\documentclass[12pt,a4paper]{article}
\usepackage{graphicx,tabularx}
\usepackage[table,xcdraw]{xcolor}
\renewcommand{\arraystretch}{1.5} 
\begin{document}

\begin{figure}
    \centering
    \includegraphics{example-image-a}
    \caption{Caption}
    \label{fig:my_label}
\end{figure}

\begin{table}
\centering
\begin{tabularx}{.6\linewidth}{rc| *{2}{>{\centering\arraybackslash}X} }
\multicolumn{1}{l}{} & \multicolumn{1}{l|}{} &   \multicolumn{2}{c}{Figure~\ref{fig:my_label} sub-plot}    \\ \hline
            Variable &         Unit          & (a)                          & (b)                          \\ \hline
                 One &           m           & \cellcolor[HTML]{FFCCC9}Vary & 50                           \\
                 Two &          kg           & 0.5                          & \cellcolor[HTML]{FFCCC9}Vary \\
               Three &          Hz           & 1000                         & 1000
\end{tabularx}
\end{table}

\end{document}

enter image description here

  • Thanks, can the (a) and (b) columns be centered? – Johnny J. L. Jul 23 '18 at 15:34
  • 1
    Sure, I edited my answer! – AboAmmar Jul 23 '18 at 15:42
3

enter image description here

complete automatically is (to my opinion) to complicated, since it depend from cells contents. this means, that you need to measure all contents and from them select the widest and to this adopt all columns width or determine table width and using tabularx table environment.

simpler is estimate the width of the widest cell (by counting number of characters) and then use this width in p{...} column type accordingly modified:

\documentclass[12pt,a4paper]{article}
\usepackage{graphicx}
\usepackage[table,xcdraw]{xcolor}
\renewcommand{\arraystretch}{1.5}

\begin{document}
    \begin{figure}
\centering
\includegraphics{example-image-duck}
\caption{Caption}
\label{fig:my_label}
    \end{figure}

    \begin{table}[htb]
\centering
\begin{tabular}{>{\raggedleft}p{3em}
                >{\centering}p{3em} |
           *{2}{>{\centering\arraybackslash}p{3em}}
                }
\multicolumn{2}{l|}{}   & \multicolumn{2}{c}{Figure~\ref{fig:my_label} sub-plot}        \\ \hline
Variable    & Unit      & (a)                           & (b)                           \\ \hline
One         & m         & \cellcolor[HTML]{FFCCC9}Vary  & 50                            \\
Two         & kg        & 0.5                           & \cellcolor[HTML]{FFCCC9}Vary  \\
Three       & Hz        & 1000                          & 1000
\end{tabular}
    \end{table}
\end{document}

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.