4

I have this LaTeX code

\documentclass{article}
\usepackage{diagbox}
\usepackage{amsmath,array,graphicx}

\begin{document}
    \begin{table}[htb]
        \centering
        \caption{Classifiaction}
        \label{table:cc}
        \resizebox{\columnwidth}{!}{%
            \begin{tabular}{|c||*{1}{c|}|p{0.4\textwidth}|*{2}{c|}}\hline
                \backslashbox{Classification}{Model}
                & Model 1 & Model 2 & Model 3 & Model 4\\\hline\hline
                Class & Class 1 & \begin{itemize}
                    \item Class 1 if $A=B$
                    \item Class 2 otherwise
                \end{itemize} & Class 2 & Class 2\\\hline
                Method & Method 1 & Method 2 & Method 1 & Method 2\\\hline
                Time & Constant & Linear & Polylogarithmic & Exponential \\\hline
            \end{tabular}
        }
    \end{table}
\end{document}

which produces a table that contains a cell with itemized bullets. The table looks ugly as the text is not centred inside the cell, there are some useless space for example in the cell with items, the lines are not equally spaced, etc.

How can I improve this table?

enter image description here

I have tried to use this method of adding itemized bullets How to use itemize in Table environment but I did not succeed, i.e., when I use \tabitem ....\\ \tabitem ... the new item is not in the same cell but in a new line.

4

i would redesign your table in the following form:

enter image description here

(red lines indicate text area borders)

\documentclass{article}
\usepackage{booktabs, multirow}
\usepackage[skip=1ex]{caption}
\usepackage{enumitem}

\begin{document}
    \begin{table}[htb]
\centering
\setlength\tabcolsep{0pt}
    \caption{Classifiaction}
    \label{table:cc}
\begin{tabular*}{\linewidth}{@{\extracolsep{\fill}} *{2}{l} p{0.28\textwidth} *{2}{l} }
    \toprule
\multirow{2.2}{*}{Classification}
        & \multicolumn{4}{c}{Model}                 \\
    \cmidrule{2-5}
        & Model 1 & Model 2 & Model 3 & Model 4     \\
        \midrule
Class   & Class 1 & \begin{itemize}[nosep, leftmargin=*,
                                    before=\vspace{-0.6\baselineskip},
                                    after =\vspace{-\baselineskip}
                                    ]
                    \item Class 1 if $A=B$
                    \item Class 2 otherwise
                    \end{itemize}
                            & Class 2 & Class 2     \\  \midrule
Method & Method 1 & Method 2 & Method 1 & Method 2  \\  \midrule
Time   & Constant & Linear   & Polylogarithmic & Exponential    \\
    \bottomrule
\end{tabular*}
    \end{table}
\end{document}

as you see, \diagbox is replaced with new row for "Model", "Classification" is placed into multirow cell.

for table is used environment tabular*, its width is equal to text width, for the list is used enumitem package. vertical lines are removed, for horizontal ones are used rules from the package booktabs.

5

You shouldn't use \resizebox with tables – it leads to inconsistent font sizes. I propose a solution with the \small fontsize, a reduced \tabcolsep, a more decent text width with the geometry package and tabularx to make the table fit between margins. I reduced the first column width , playing with the height and width parameeters of \diagbox. For the liqst problem, I think it will look nicer left aligned at the left border of the cell, and without verical spacing. Last, I replaced \hline with \ hhline to have nicer intersections between vertical and horizontal rules.

\documentclass{article}
\usepackage[showframe]{geometry} 
\usepackage{amsmath, graphicx} 
\usepackage{diagbox}
\usepackage{array, caption, tabularx, hhline}
\usepackage{enumitem} 

\makeatletter
\newcommand*{\compress}{\@minipagetrue}
\makeatother

\begin{document}

    \begin{table}[htb]
    \centering\small
    \setlength{\extrarowheight}{2pt}
    \caption{Classification}
     \setlength{\tabcolsep}{2pt}
    \label{table:cc}
        \begin{tabularx}{\linewidth}{|c||*{1}{c|}| >{\compress}X|*{2}{c|}}
        \hhline{-||-||---}
            \diagbox[height=7ex, width=9em]{Classification}{\raisebox{-1.6ex}{Model}}
            & Model 1 & Model 2 & Model 3 & Model 4\\
            \hhline{=::=::===}
            Class & Class 1 & \begin{itemize}[wide=0pt, leftmargin=*, topsep=0pt, itemsep =0pt, after=\vspace{-\baselineskip}]
                \item Class 1 if $A=B$
                \item Class 2 otherwise
            \end{itemize} & Class 2 & Class 2\\
        \hhline{-||-||---}
            Method & Method 1 & Method 2 & Method 1 & Method 2\\
        \hhline{-||-||---}
            Time & Constant & Linear & Polylogarithmic & Exponential \\
        \hhline{-||-||---}
        \end{tabularx}
    \end{table}

\end{document} 

enter image description here

4

Like this:

\documentclass{article}
\usepackage{diagbox}
\usepackage{amsmath,array,graphicx}

\begin{document}
\renewcommand*{\arraystretch}{2}
    \begin{table}[htb]
        \centering
        \caption{Classifiaction}
        \label{table:cc}
        \resizebox{\columnwidth}{!}{%
            \begin{tabular}{|c||*{1}{c|}|>\centering m{0.4\textwidth}|*{2}{c|}}\hline
                \backslashbox{Classification}{Model}
                & Model 1 & Model 2 & Model 3 & Model 4\\\hline\hline
                Class & Class 1 & \begin{itemize}
                    \item Class 1 if $A=B$
                    \item Class 2 otherwise
                \end{itemize} & Class 2 & Class 2\\\hline
                Method & Method 1 & Method 2 & Method 1 & Method 2\\\hline
                Time & Constant & Linear & Polylogarithmic & Exponential \\\hline
            \end{tabular}
        }
    \end{table}
\end{document}
  • Thanks. Can I increase the height of the third and fourth rows? – Zir Oct 9 '18 at 18:10
  • Also, can I center (in the middle) the contents of the cell containing the items? – Zir Oct 9 '18 at 18:14
  • with \renewcommand{\arraystretch}{2} for example the height of every row is doubled. – Hafid Boukhoulda Oct 9 '18 at 18:28
4

Here's a solution that seems to provide what you want:

enter image description here

There are three keys here to what I'm doing:

  • I've save the list in a box and prior to making the table. In the table I can then apply \usebox to the box to obtain the already formatted content. By doing this, I can make the third column centers (as it appears you want). Using the minipage removes the unwanted white space from before and after the itemize list (provided there's no content outside of the list.
  • By using \raisebox, I'm basically able to give the appearance of adding a buffer of white space above and below the list. This is a bit of a misnomer since what I've really done is just told LaTeX that the box's height and depth are a bit larger than they really are. The syntax for \raisebox is
\raisebox{<lift>}
        [<optional height specification>]
        [<optional depth specification>]
        {<content>}
  • I use struts to create some control over the spacing.

Here's the code:

\documentclass{article}
\usepackage{diagbox}
\usepackage{amsmath,array,graphicx}

\newcommand\aestrut[2]{\rule[#1]{0pt}{1pt}%%
                       \rule[#2]{0pt}{1pt}}
\newcommand\fixedstrut{\aestrut{2.5ex}{-1.5ex}}

\begin{document}
\newsavebox\aebox
\begin{lrbox}\aebox
  \begin{minipage}{0.4\textwidth-6pt}
    \begin{itemize}
      \item Class 1 if $A=B$
      \item Class 2 otherwise
    \end{itemize} 
  \end{minipage}%%
\end{lrbox}

\begin{table}[htb]
  \centering
  \caption{Classifiaction\aestrut{0pt}{-2ex}}
  \label{table:cc}
  \resizebox{\columnwidth}{!}{%
    \begin{tabular}{|c||*{1}{c|}|c|*{2}{c|}}\hline
      \backslashbox{\fixedstrut Classification}
                   {\fixedstrut Model} & 
      Model 1                          & 
      Model 2                          & 
      Model 3                          & 
      Model 4  
      \\\hline\hline
      Class                            & 
      Class 1                          & 
      \raisebox{0pt}[\height+2ex][\depth+2.5ex]{\usebox{\aebox}}
                                       & 
      Class 2                          & 
      Class 2  
      \\\hline
      \aestrut{3ex}{-2ex}%%
      Method                           & 
      Method 1                         & 
      Method 2                         & 
      Method 1                         & 
      Method 2
      \\\hline
      \aestrut{3ex}{-2ex}%%
      Time                             & 
      Constant                         & 
      Linear                           & 
      Polylogarithmic                  & 
      Exponential 
      \\\hline
    \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.