1

I wanted to ask how I center the text in the description row (not in the rest of the table that will follow. This to reduce the space between the text and the \cmidrule

\documentclass[a4paper, 11pt]{article}
\usepackage[left=2cm,top=2cm,right=2cm, bottom=2cm]{geometry}
\usepackage[utf8]{inputenc} 
\usepackage{caption}
\usepackage{siunitx}
\usepackage{ragged2e}
\usepackage{booktabs, tabularx} 
\newcolumntype{C}{>{\RaggedRight\hspace{0pt}\arraybackslash}X}
\newcolumntype{P}[1]{>{\RaggedRight\hspace{0pt}}p{#1}}
\newlength\colwidth
\usepackage{float}  
\usepackage[version=4]{mhchem}


\begin{document}


 \begin{table}[H]
\settowidth\colwidth{Solutionn} 
\begin{tabularx}{\textwidth}{@{}P{\colwidth}*  {7}{C} @{}}
\toprule 
\textbf{Solution}     &      \multicolumn{3}{c}{\textbf{amount of substance [$\si{\mol}$]}  }     &       \multicolumn{3}{c}{\textbf{mass of substance [$\si{\gram}$]}  }     &\textbf{Volume of Stock [$\si{\milli\liter}$]}      \\
\cmidrule(lr){2-4} \cmidrule(lr){5-7}
 & $\ce{KH2PO4}$ & $\ce{Na2HPO4}$ & $\ce{NaCl}$ & $\ce{KH2PO4}$ & $\ce{Na2HPO4}$ & $\ce{NaCl}$\\
\midrule
A  \\
\bottomrule
\end{tabularx}
\endtabularx
\end{table}

\end{document}

pic

1
  • 3
    Off-topic: It's not necessary to place \ce{...} macros in math mode. Also, left=2cm,top=2cm,right=2cm, bottom=2cm can be expressed more succinctly as margin=2cm.
    – Mico
    Apr 22, 2021 at 6:16

2 Answers 2

2

I think that you looking for the following:

enter image description here

By use of \multirow package you can define cell which span two (or more) text lines in the adjacent cells in spanned rows. For horisontal centering of text in some cells, you can use \multicolumn{1}{c}{...} command.

In MWE below are also considered @Mico comments below your question:

\documentclass[a4paper, 11pt]{article}
\usepackage[margin=2cm]{geometry}
\usepackage{caption}
\usepackage{siunitx}
\usepackage{ragged2e}
\usepackage{booktabs, multirow, tabularx}
\newcolumntype{L}[1]{>{\RaggedRight\hspace{0pt}\arraybackslash%
                     \hsize=#1\hsize}X}
\newcolumntype{P}[1]{>{\RaggedRight\hspace{0pt}}p{#1}}
\NewExpandableDocumentCommand\mcc{O{c}m}{\multicolumn{1}{#1}{#2}}
\newlength\colwidth
\usepackage{float}
\usepackage[version=4]{mhchem}

\begin{document}
    \begin{table}[ht]   % <---
    \settowidth\colwidth{Solutionn}
    \setlength\tabcolsep{3pt}
\begin{tabularx}{\textwidth}{@{} P{\colwidth} *{6}{L{0.98}} L{1.12} @{}}
    \toprule
\multirow{2.4}{=}{\textbf{Solution}}
    & \multicolumn{3}{c}{\textbf{amount of substance [\si{\mol}]} }     
        & \multicolumn{3}{c}{\textbf{mass of substance [\si{\gram}]} }     
            & \multirow{2.4}{=}{\textbf{Volume of Stock [\si{\milli\liter}]}}\\
    \cmidrule(lr){2-4} \cmidrule(lr){5-7}
    & \mcc{\ce{KH2PO4}} & \mcc{\ce{Na2HPO4}} & \mcc{\ce{NaCl}} 
    & \mcc{\ce{KH2PO4}} & \mcc{\ce{Na2HPO4}} & \mcc{\ce{NaCl}}    &    \\
    \midrule
A  &    &   &   &   &   &   &                       \\
    \bottomrule
\end{tabularx}
%\endtabularx % superfluous 
    \end{table}
\end{document}
2

It's handy to create a shortcut macro, say,

\newcolumntype{C}{>{\Centering\hspace{0pt}}X}

and write \mC{\ce{KH2PO4}} to override the default formatting for that cell only.

enter image description here

\documentclass[a4paper, 11pt]{article}
\usepackage[margin=2cm]{geometry}
\usepackage[utf8]{inputenc} 
\usepackage[T1]{fontenc}
\usepackage{caption,siunitx,ragged2e,booktabs,tabularx} 
\newcolumntype{L}{>{\RaggedRight\hspace{0pt}}X}
\newcolumntype{C}{>{\Centering\hspace{0pt}}X}
\newcolumntype{P}[1]{>{\RaggedRight\hspace{0pt}}p{#1}}
\usepackage[version=4]{mhchem}

\newlength\colwidth
\newcommand\mC[1]{\multicolumn{1}{C}{#1}} % handy shortcut macro

\begin{document}

\begin{table}[ht!]
\settowidth\colwidth{\textbf{Solution}} % <-- need to use \textbf to get correct measurement
\begin{tabularx}{\textwidth}{@{} P{\colwidth} *{7}{L} @{}}
\toprule 
\textbf{Solution} &  
\multicolumn{3}{c}{\textbf{Amount of substance} [\si{\mol}]}   &       
\multicolumn{3}{c}{\textbf{Mass of substance} [\si{\gram}]}   &
\multicolumn{1}{C@{}}{\textbf{Volume of stock} [\si{\milli\liter}]}      \\
\cmidrule(lr){2-4} \cmidrule(lr){5-7}
& \mC{\ce{KH2PO4}} & \mC{\ce{Na2HPO4}} & \mC{\ce{NaCl}}
& \mC{\ce{KH2PO4}} & \mC{\ce{Na2HPO4}} & \mC{\ce{NaCl}} \\
\midrule
A \\
B \\
\bottomrule
\end{tabularx}
\end{table}

\end{document}
3
  • When I would like to center the description using \multirow{2}{*}{\textbf{Solution}} and instead write Solution description. How can I bring description to a new line because using \\ or \linebreak it does not work
    – Elia
    Apr 23, 2021 at 14:05
  • @Elia - I'm very sorry, but I'm not sure I understand what you're trying to achieve. Please clarify.
    – Mico
    Apr 23, 2021 at 18:26
  • I posted a new question because I think it is not related to the question I wrote above otherwise its confusing for someone who reads it
    – Elia
    Apr 23, 2021 at 19:17

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .