2

I am using the tabularx package to customize my tables. I try to define newcolumn type "C" and "Y". As I am using arraybackslash, the bold 'titles' of my table are aligned quite ugly. How can I fix this? See my figure below. The text "Prvek" and "Kov" is not aligned vertically. The text in third column is wrapped to second line. Also see my MNWE.

I also try something like:

\newcolumntype{C}[1]{>{\centering\arraybackslash\vspace{0pt}\setlength{\parskip}{0pt plus 1pt}}m{#1}<{\vspace{0pt}}}

but it doesn't work.

enter image description here MNWE:

\documentclass[twocolumn,czech]{scrbook}

\usepackage{tabularx}
\usepackage{siunitx}
\usepackage[version=4]{mhchem}
\usepackage[table]{xcolor}
\definecolor{CornflowerBlue}{HTML}{ADD8E6}   % CornflowerBlue
\usepackage{lipsum}

\newcolumntype{Y}{>{\centering\arraybackslash}X}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}


\begin{document}
\section{test}
\lipsum[4]
  \begin{table}[h!]
    \centering
    \rowcolors{2}{lightgray}{white}
    \begin{tabularx}{\linewidth}{|C{1.1cm}|C{1.7cm}|Y|}
      \hline
      \rowcolor{CornflowerBlue}
      \textbf{\color{black} Prvek} & \textbf{\color{black} Kov} & \textbf{\color{black} Měrná vodivost 
      (\unit{\siemens\per\meter})}             \\ \hline
        \ce{Ag} & Stříbro   & \num{63.012e6}   \\ \hline
        \ce{Cu} & Měď       & \num{58.108e6}   \\ \hline
        \ce{Au} & Zlato     & \num{45.167e6}   \\ \hline
        \ce{Al} & Hliník    & \num{34.795e6}   \\ \hline
        \ce{Be} & Beryllium & \num{27.778e6}   \\ \hline
        \ce{Fe} & Železo    & \num{10.406e6}   \\ \hline
        \ce{Ni} & Nikl      & \num{14.430e6}   \\ \hline
        \ce{Zn} & Zinek     & \num{16.949e6}   \\ \hline
        \ce{Sn} & Cín       & \num{8.696e6}    \\ \hline
    \end{tabularx}
  \end{table}
\end{document}

4 Answers 4

1

Your answer can be simplified as follows:

\documentclass[twocolumn,czech]{scrbook}

\usepackage{lipsum}
\usepackage{tabularx}
\usepackage{siunitx}
\usepackage[version=4]{mhchem}
\usepackage[table]{xcolor}
\definecolor{CornflowerBlue}{HTML}{ADD8E6}   % CornflowerBlue

\newcolumntype{C}{>{\centering\arraybackslash\hspace{0pt}}X}

\begin{document}
\section{test}
\lipsum[4]
  \begin{table}[h!]
  \renewcommand\tabularxcolumn[1]{m{#1}}
  \rowcolors{2}{lightgray}{white}
    \begin{tabularx}{\linewidth}{|c|c|C|}
      \hline
      \rowcolor{CornflowerBlue}
      \textbf{Prvek} 
                & \textbf{Kov} 
                            & \textbf{Měrná vodivost
                              (\unit{\siemens\per\meter})}    \\ \hline
        \ce{Ag} & Stříbro   & \num{63.012e6}   \\ \hline
        \ce{Cu} & Měď       & \num{58.108e6}   \\ \hline
        \ce{Au} & Zlato     & \num{45.167e6}   \\ \hline
        \ce{Al} & Hliník    & \num{34.795e6}   \\ \hline
        \ce{Be} & Beryllium & \num{27.778e6}   \\ \hline
        \ce{Fe} & Železo    & \num{10.406e6}   \\ \hline
        \ce{Ni} & Nikl      & \num{14.430e6}   \\ \hline
        \ce{Zn} & Zinek     & \num{16.949e6}   \\ \hline
        \ce{Sn} & Cín       & \num{8.696e6}    \\ \hline
    \end{tabularx}
  \end{table}
\end{document}

enter image description here

Or with use of tblr from tabularray package code is even more concise:

\documentclass[twocolumn,czech]{scrbook}

\usepackage{lipsum}
\usepackage[version=4]{mhchem}
\usepackage{xcolor}
\definecolor{CornflowerBlue}{HTML}{ADD8E6}   % CornflowerBlue
\usepackage{tabularray}
\UseTblrLibrary{siunitx}

\begin{document}
\section{test}
\lipsum[4]
  \begin{table}[h!]
    \begin{tblr}{hline{1,2,Z}, 
                 colspec = {c c X[c,m, si={table-format=2.3e1}]},
                 row{1}  = {guard, font=\bfseries, bg=CornflowerBlue},
                 row{odd[3]} = {bg=lightgray}
                 }
Prvek       & Kov       & Měrná vodivost
                          (\unit{\siemens\per\meter})    \\ 
    \ce{Ag} & Stříbro   & 63.012e6   \\
    \ce{Cu} & Měď       & 58.108e6   \\
    \ce{Au} & Zlato     & 45.167e6   \\
    \ce{Al} & Hliník    & 34.795e6   \\
    \ce{Be} & Beryllium & 27.778e6   \\
    \ce{Fe} & Železo    & 10.406e6   \\
    \ce{Ni} & Nikl      & 14.430e6   \\
    \ce{Zn} & Zinek     & 16.949e6   \\
    \ce{Sn} & Cín       &  8.696e6   \\
    \end{tblr}
  \end{table}
\end{document}

enter image description here

0

The solution could be achieved with command \renewcommand{\arraystretch}{1.5} I also encapsulate the tabularx with tabular environment. Thus the code is:

\begin{table}[ht!]
  \renewcommand{\arraystretch}{1.5}
  \begin{tabularx}{\linewidth}{|z{2cm}|X|m{2cm}|}
  \hline
  \textbf{\color{black} Prvek} & \textbf{\color{black} Kov} & \textbf{\color{black} Měrná vodivost (\unit{\siemens\per\meter})}      \\ \hline
    \ce{Ag} & Stříbro   & \num{63.012e6}   \\ \hline
    \ce{Cu} & Měď       & \num{58.108e6}   \\ \hline
    \ce{Au} & Zlato     & \num{45.167e6}   \\ \hline
    \ce{Al} & Hliník    & \num{34.795e6}   \\ \hline
    \ce{Be} & Beryllium & \num{27.778e6}   \\ \hline
    \ce{Fe} & Železo    & \num{10.406e6}   \\ \hline
    \ce{Ni} & Nikl      & \num{14.430e6}   \\ \hline
    \ce{Zn} & Zinek     & \num{16.949e6}   \\ \hline
    \ce{Sn} & Cín       & \num{8.696e6}    \\ \hline
  \end{tabularx}
\end{table}

Similarly, I tried to set \extrarowheight values for individual rows in the "tabularx" environment, using the \setlength command along with the commands to create the "tabularx" environment. But in this case (\setlength{\extrarowheight}{4ex}) the text inside the rows was not vertically aligned. I haven't figured out an easy way to fix it. But the first solution works reliably.

0

Here is a way to construct that tabular with {NiceTabular} of nicematrix. You need several compilations.

\documentclass[twocolumn,czech]{scrbook}
\usepackage{siunitx}
\usepackage{lipsum}
\usepackage{nicematrix}

\begin{document}
\section{test}
\lipsum[4]
  \begin{table}[h!]
    \centering
    \begin{NiceTabular}
      [hvlines,cell-space-limits=3pt,color-inside]
      {w{c}{1.1cm}w{c}{1.7cm}X[c]}
      \RowStyle[rowcolor=[HTML]{ADD8E6}]{\bfseries}
          Prvek & Kov       & \Block{}{Měrná vodivost\\ (\unit{\siemens\per\meter})} \\ 
      \rowcolors{lightgray}{white}
          Ag    & Stříbro   & \num{63.012e6}   \\ 
          Cu    & Měď       & \num{58.108e6}   \\ 
          Au    & Zlato     & \num{45.167e6}   \\ 
          Al    & Hliník    & \num{34.795e6}   \\ 
          Be    & Beryllium & \num{27.778e6}   \\ 
          Fe    & Železo    & \num{10.406e6}   \\ 
          Ni    & Nikl      & \num{14.430e6}   \\ 
          Zn    & Zinek     & \num{16.949e6}   \\ 
          Sn    & Cín       & \num{8.696e6}    \\ 
    \end{NiceTabular}
  \end{table}

\end{document}

Output of the above code

0

No need of complicated code. Here are three realizations, one according to your wish, two in a better fashion: colors don't really add to clarity, as well as boldface. For the first realization, just nest a plain tabular.

Of course a caption is needed.

\documentclass[twocolumn,czech]{scrbook}
\usepackage[T1]{fontenc}
\usepackage{babel}

\usepackage{array}
\usepackage{siunitx}
\usepackage[version=4]{mhchem}
\usepackage[table]{xcolor}

\usepackage{booktabs} % for the second realization

\usepackage{lipsum}

\definecolor{CornflowerBlue}{HTML}{ADD8E6}   % CornflowerBlue

\newcolumntype{Y}{>{\centering\arraybackslash}X}
\newcolumntype{C}[1]{>{\centering\let\newline\\\arraybackslash\hspace{0pt}}m{#1}}


\begin{document}
\section{test}

\lipsum[1][1-2]

\begin{table}[htp]
\centering
\rowcolors{2}{lightgray}{white}

\begin{tabular}{
  |w{c}{1.1cm}|
   w{c}{1.7cm}|
   w{c}{\dimeval{\columnwidth-2.8cm-6\tabcolsep-4\arrayrulewidth}}|
}
\hline
\rowcolor{CornflowerBlue}
\color{black}\bfseries Prvek &
\color{black}\bfseries Kov &
\color{black}\begin{tabular}{@{}c@{}}
  \bfseries Měrná vodivost \\ (\unit{\siemens\per\meter})
\end{tabular} \\
\hline
\ce{Ag} & Stříbro   & \num{63.012e6}   \\ \hline
\ce{Cu} & Měď       & \num{58.108e6}   \\ \hline
\ce{Au} & Zlato     & \num{45.167e6}   \\ \hline
\ce{Al} & Hliník    & \num{34.795e6}   \\ \hline
\ce{Be} & Beryllium & \num{27.778e6}   \\ \hline
\ce{Fe} & Železo    & \num{10.406e6}   \\ \hline
\ce{Ni} & Nikl      & \num{14.430e6}   \\ \hline
\ce{Zn} & Zinek     & \num{16.949e6}   \\ \hline
\ce{Sn} & Cín       & \num{8.696e6}    \\ \hline
\end{tabular}

\end{table}

\lipsum[1-2]

\begin{table}[htp]
\centering

\begin{tabular*}{\columnwidth}{@{\extracolsep{\fill}}ccS[table-format=2.3e1]@{}}
\toprule
Prvek & Kov & {\begin{tabular}[t]{@{}c@{}}
  Měrná vodivost \\ (\unit{\siemens\per\meter})
\end{tabular}} \\
\midrule
\ce{Ag} & Stříbro   & 63.012e6 \\
\ce{Cu} & Měď       & 58.108e6 \\
\ce{Au} & Zlato     & 45.167e6 \\
\ce{Al} & Hliník    & 34.795e6 \\
\ce{Be} & Beryllium & 27.778e6 \\
\ce{Fe} & Železo    & 10.406e6 \\
\ce{Ni} & Nikl      & 14.430e6 \\
\ce{Zn} & Zinek     & 16.949e6 \\
\ce{Sn} & Cín       &  8.696e6 \\
\bottomrule
\end{tabular*}

\end{table}

\lipsum[1-2]

\begin{table}[htp]
\centering

\begin{tabular}{@{}ccS[table-format=2.3e1]@{}}
\toprule
Prvek & Kov & {\begin{tabular}[t]{@{}c@{}}
  Měrná vodivost \\ (\unit{\siemens\per\meter})
\end{tabular}} \\
\midrule
\ce{Ag} & Stříbro   & 63.012e6 \\
\ce{Cu} & Měď       & 58.108e6 \\
\ce{Au} & Zlato     & 45.167e6 \\
\ce{Al} & Hliník    & 34.795e6 \\
\ce{Be} & Beryllium & 27.778e6 \\
\ce{Fe} & Železo    & 10.406e6 \\
\ce{Ni} & Nikl      & 14.430e6 \\
\ce{Zn} & Zinek     & 16.949e6 \\
\ce{Sn} & Cín       &  8.696e6 \\
\bottomrule
\end{tabular}

\end{table}

\lipsum

\end{document}

First two realizations:

enter image description here

Third possibility (no stretching):

enter image description here

You must log in to answer this question.

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