5

I would like to have the content in the first and second columns are centered aligned within its respected cells in a table with two column paper template as follows.

expected

The current table is as follows. current

Here is the code:

\documentclass[journal]{IEEEtran}
\usepackage{caption}
\usepackage{tabularx}
\begin{document}

\begin{table}[h] \captionof{table}{Table of $q$ and $z$} \label{tab:table_q_and_z} \noindent \begin{tabularx}{\columnwidth} { | c | c | >{\raggedright\arraybackslash}X |} \hline \textbf{Steps} & \textbf{$q_i$} & \textbf{Definition of $z_i$} \ \hline \multirow{$i \leq (n-1)$} & $q_i$ = 0 & $z_i$ = number of \emph{emptied} chambers at the \emph{cleaning} place $i \leq (n-1)$ \ \cline{2-3} & \centering $q_i$ = 1 & $z_i$ = number of \emph{loaded} chambers at the \emph{processing} place $i \leq (n-1)$ \ \hline $i=n$ & $q_{n}$ = 0 & $z_n$ = number of \emph{loaded} chambers at the \emph{processing} place $n$ \ \cline{2-3} & $q_{n}$ = 1 & $z_{n}$ = number of \emph{emptied} chambers at the \emph{cleaning} place $n$ \ \hline \end{tabularx} \end{table} \end{document}

What should be added to the current code?

  • Off-topic: Since the IEEEtran class uses Times Roman as the default text font, you may want to consider executing \usepackage{newtxtext,newtxmath} in order to get a compatible Times Roman math font. – Mico Apr 02 '23 at 11:15

3 Answers3

4

To achieve vertical centering of the material in the c-type columns, I suggest you execute

\renewcommand\tabularxcolumn[1]{m{#1}}

This is explained in more detail in the user guide of the tabularx package.

I'd also suggest you complete the \multirow commands and -- in the header row -- change \textbf{$q_i$} to \boldmath$q_i$ and change \textbf{Definition of $z_i$} to \boldmath\bfseries Definition of $z_i$, respectively. And, please change $q_i$ = 1 to $q_i = 1$, etc in column 2. I'd further suggest to get rid of the specious \centering instruction and change \captionof{table}{...} to just \caption{...}. (There's no need for \captionof since you're using a table environment.) And, last but not least, consider adding the instruction \setlength\extrarowheight{2pt} to that the table gets a bit less of a compressed "look".

enter image description here

\documentclass[journal]{IEEEtran}
\usepackage{caption}
\usepackage{tabularx}
\renewcommand\tabularxcolumn[1]{m{#1}} % <-- new
\usepackage{multirow}
\begin{document}

\begin{table}[h] \setlength\extrarowheight{2pt} \caption{Table of $q$ and $z$} \label{tab:table_q_and_z} \noindent \begin{tabularx}{\columnwidth} { | c | c | >{\raggedright\arraybackslash}X |} \hline \textbf{Steps} & \boldmath$q_i$ & \boldmath\bfseries Definition of $z_i$ \ \hline \multirow{3}{}{$i \leq (n-1)$}
& $q_i=0$ & $z_i$ = number of \emph{emptied} chambers at the \emph{cleaning} place $i \leq (n-1)$ \ \cline{2-3} & $q_i=1$ & $z_i$ = number of \emph{loaded} chambers at the \emph{processing} place $i \leq (n-1)$ \ \hline \multirow{3}{
}{$i=n$}
& $q_n=0$ & $z_n$ = number of \emph{loaded} chambers at the \emph{processing} place $n$ \ \cline{2-3} & $q_n=1$ & $z_{n}$ = number of \emph{emptied} chambers at the \emph{cleaning} place $n$ \ \hline \end{tabularx} \end{table} \end{document}


Addendum to address some additional issues with the table:

  • The IEEEtran document class is only barely compatible with the caption package. Don't use this package unless the journal you are planning to submit a paper to expressly allows you to use this package.

  • The IEEEtran class uses Times Roman as the default text font, but Computer Modern as the default math font. These two fonts are not all that well matched, visually/aesthetically speaking. I'd recommend executing \usepackage{newtxtext,newtxmath} in the preamble to use Times Roman for both text and math.

  • Consider getting rid of the all verical lines and most horizontal lines in the table, in order to give the table a much more open and inviting "look". I suggest you use the line-drawing macros of the booktabs package instead of \hline and \cline.

  • Using bold-facing in the header row doesn't achieve much, except run the risk of giving the entire table a heavy-handed and potentially vulgar look. Don't over-use bold-facing; for the table at hand, I don't think any bold-facing is needed.

Implementing these ideas, dropping vertical centering in columns 1 and 2, and left-aligning the material in columns 1 and 2, would create the following look. I hope you will agree with me that the table looks a whole lot more inviting and appealing than the one posted above.

enter image description here

\documentclass[journal]{IEEEtran}
\usepackage{tabularx}
\usepackage{newtxtext,newtxmath}
\usepackage{booktabs} % for well-spaced horizontal rules
\usepackage{lipsum}   % filler text

\begin{document}

\begin{table}[h]

\caption{Table of $q$ and $z$} \label{tab:table_q_and_z}

\begin{tabularx}{\columnwidth} { @{} lll @{\hspace{\tabcolsep}} >{\raggedright\arraybackslash}X @{}}

\toprule Steps & $q_i$ & \multicolumn{2}{l}{Definitions of $z_i$ and $z_n$}\

\midrule

$i\leq n-1$ & $q_i=0$ & $z_i$ & number of \emph{emptied} chambers at the \emph{cleaning} place $i \leq (n-1)$ \

& $q_i=1$ & $z_i$ & number of \emph{loaded} chambers at the \emph{processing} place $i \leq (n-1)$ \

\addlinespace

$i=n$ & $q_n=0$ & $z_n$ & number of \emph{loaded} chambers at the \emph{processing} place $n$ \

& $q_n=1$ & $z_n$ & number of \emph{emptied} chambers at the \emph{cleaning} place $n$ \

\bottomrule

\end{tabularx} \end{table}

\lipsum

\end{document}

Mico
  • 506,678
4

The package nicematrix provides an environment {NiceTabularX} similar to the environment {tabularx} (of the package tabularx) but with, among other features, the ability to add options to the columns X. With X[l,m], you will have a X column which behaves as a m column with a horizontal alignment on the left.

\documentclass[journal]{IEEEtran}
\usepackage{caption}
\usepackage{nicematrix}
\begin{document}

\begin{table}[h] \captionof{table}{Table of $q$ and $z$} \label{tab:table_q_and_z} \noindent \begin{NiceTabularX}{\columnwidth} {|c|c|X[m,l]|} \hline \textbf{Steps} & \textbf{$q_i$} & \textbf{Definition of $z_i$} \ \hline \Block{2-1}{$i \leq (n-1)$} & $q_i$ = 0 & $z_i$ = number of \emph{emptied} chambers at the \emph{cleaning} place $i \leq (n-1)$ \ \cline{2-3} & \centering $q_i$ = 1 & $z_i$ = number of \emph{loaded} chambers at the \emph{processing} place $i \leq (n-1)$ \ \hline \Block{2-1}{$i=n$} & $q_{n}$ = 0 & $z_n$ = number of \emph{loaded} chambers at the \emph{processing} place $n$ \ \cline{2-3} & $q_{n}$ = 1 & $z_{n}$ = number of \emph{emptied} chambers at the \emph{cleaning} place $n$ \ \hline \end{NiceTabularX} \end{table} \end{document}

Output of the above code

However, I recommend a tabular with booktabs in the spirit of booktabs (cf. Mico's answer).

F. Pantigny
  • 40,250
3

As complement to nice @Mico answer (+1), but with use of the tabularray and bm packages:

  • code is a bit shorter
  • tabularray syntax for column specification enabled to define math column and simple vertical centering of cells content (option m)
  • table lines are defined in table preamble
\documentclass[journal]{IEEEtran}
\usepackage{tabularray}
\usepackage{bm}

\begin{document} \begin{table}[h] \caption{Table of $q$ and $z$} \label{tab:table_q_and_z} \begin{tblr}{hlines, vlines, colspec = { *{2}{Q[c, mode=math]} X[l, m]}, row{1} = {font=\bfseries, mode=text}, rowsep = 3pt, } Steps & $\bm{q_i}$ & Definition of $\bm{z_i}$ \ \SetCell[r=2]{c} i \leq (n-1) & q_i=0 & $z_i$: number of \emph{emptied} chambers at the \emph{cleaning} place $i \leq (n-1)$ \ & q_i=1 & $z_i$: number of \emph{loaded} chambers at the \emph{processing} place $i \leq (n-1)$ \ \SetCell[r=2]{c} i=n & q_n=0 & $z_n$: number of \emph{loaded} chambers at the \emph{processing} place $n$ \ & q_n=1 & $z_{n}$: number of \emph{emptied} chambers at the \emph{cleaning} place $n$ \ \end{tblr} \end{table} \end{document}

enter image description here

Zarko
  • 296,517