1

Could you please implement this table in latex for me? I need the table to fit within the text boundaries. The actual table has a quite a lot of information. A, b and c just an example.

table

enter image description here

3
  • Welcome to TeX.SX! It is difficult to answer your question meaningfully without knowing more about the contents of the cells. With your sample data that table fits easily within the text boundaries. To achieve the same with your real data, one has to choose appropriate cell types depending on the contents. Read e.g. de.sharelatex.com/learn/Tables to learn the basics. You might also want to try to draft the table with a tool like tablesgenerator.com (I have never used it, but it may be a starting point). If you get stuck, return to this site and ask specific questions.
    – gernot
    Mar 29, 2017 at 12:27
  • Thanks. i meant text auto-fit in cells like in Word. And the table still within the text such as in the questions here (has same width as text).
    – MWH
    Mar 29, 2017 at 12:51
  • See tabularx and enumitem (for lists).
    – Bernard
    Mar 29, 2017 at 13:04

3 Answers 3

4

This is a solution with new I column type, which is a p column for which entering and leaving a cell enters and leaves an itemize environment with suitable parameters, so that you only have to type your items.

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{enumitem}
\usepackage{ragged2e} 
\usepackage{array, tabularx}
\usepackage{makecell}
\setcellgapes{3pt}

\newcolumntype{I}[1]{>{\arraybackslash\itemize[wide=0pt, leftmargin=*, nosep, itemsep=0pt, before=\leavevmode\vspace{-\baselineskip}, after=\vspace*{-\baselineskip}]}p{0.3\linewidth}<{\enditemize}}

\begin{document}

\begin{table}[!htb]
\sffamily\makegapedcells
  \begin{tabularx}{\linewidth}{|*{2}{>{\RaggedRight\arraybackslash}X|}I{\0.333\linewidth}|}
    \hline
    \multicolumn{2}{|c|}{XXX} & \multicolumn{1}{c|}{X} \\
    \hline
    A & A1 & \item A1.1
    \item A1.2
    \item A1.3 \\
    \cline{2-3}
      & A2 & \item A2 \\
    \cline{2-3}
      & A3 & \item A3 \\
    \hline
    B & B1 & \item B1.1
    \item B1.2
    \item B1.3 \\
    \cline{2-3}
      & B2 & \item B2.1%
    \item B2.2%
    \\
    \cline{2-3}
      & B3 & \item B3.1
    \item B3.2\\
    \cline{2-3}
      & B4 & \item B4 \\
    \cline{2-3}
      & B5 & \item B5 \\
    \cline{2-3}
      & B6 & \item B6 \\
    \hline
    C & C1 & \item C1.1
    \item C1.2 \\
    \cline{2-3}%
      & C1 & \item C2.1
    \item C2.2 \\
    \hline
  \end{tabularx}
\end{table}

\end{document} 

enter image description here

Edit:

Here is a variant code to satisfy your last requirements (and add some fanciness):

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[showframe]{geometry}
\usepackage{enumitem}
\usepackage{ragged2e}
\usepackage{array, tabularx, ltablex, booktabs}%
\keepXColumns
\usepackage{makecell}
\usepackage[table, svgnames]{xcolor}
\setcellgapes{3pt}

\usepackage{enumitem, etoolbox}
\AtBeginEnvironment{table}{\setlist[itemize, 1]{wide=0pt, leftmargin= *, topsep=0pt, itemsep=0pt, labelsep=.333em,
before=\leavevmode\vspace*{-\baselineskip},after=\vspace*{-\baselineskip}}}

\newcolumntype{L}{ >{\itemize}X<{\enditemize}}

\newcolumntype{I}[1]{>{\arraybackslash\itemize[wide=0pt, leftmargin=*, nosep, itemsep=0pt, before=\leavevmode\vspace{-\baselineskip}, after=\vspace*{-\baselineskip}]}p{0.3\linewidth}<{\enditemize\leavevmode}}

\begin{document}

\begin{table}[!htb]\noindent%\centering
  \sffamily\makegapedcells\arrayrulecolor{IndianRed}
  \setlength\aboverulesep{0pt}
  \setlength\belowrulesep{0pt}
  \begin{tabularx}{1.07\linewidth}{!{\color{IndianRed}\vrule width 1.5pt}>{\RaggedRight\arraybackslash\hsize=0.6\hsize}X !{\color{IndianRed}\vrule width \heavyrulewidth}>{\RaggedRight\arraybackslash\hsize=1.\hsize}X%
    !{\color{IndianRed}\vrule width \heavyrulewidth}>{\RaggedRight\arraybackslash\hsize=1.4\hsize}L!{\color{IndianRed}\vrule width1.5pt}}
    \toprule[1.5pt]
    \multicolumn{2}{!{\color{IndianRed}\vrule width 1.5pt}c !{\color{IndianRed}\vrule width \heavyrulewidth}}{XXX} & \multicolumn{1}{c!{\color{IndianRed}\vrule width 1.5pt}}{X} \\
    \midrule[\heavyrulewidth]
    A & A1 &
    \item A1.1
    \item A1.2
    \item A1.3
    \\%
    \cline{2-3}
      & A2 & \item A2 \\
    \cline{2-3}
      & A3 & \item A3 \\
    \midrule[\heavyrulewidth]
    B & B1 & \item B1.1
    \item B1.2
    \item B1.3 \\
    \cline{2-3}
      & B2 & \item B2.1%
    \item B2.2%
    \\
    \cline{2-3}
      & B3 & \item B3.1
    \item B3.2\\
    \cline{2-3}
      & B4 & \item B4 \\
    \cline{2-3}
      & B5 & \item B5 \\
    \cline{2-3}
      & B6 & \item B6 \\
    \midrule[\heavyrulewidth]
    C & C1 & \item C1.1
    \item C1.2 \\
    \cline{2-3}%
      & C1 & \item C2.1
    \item C2.2 \\
    \bottomrule[1.5pt]
  \end{tabularx}
\end{table}

\end{document} 

enter image description here

6
  • Thanks a lot Bernard. It works fine and excellent!. Could i change the font size to be smaller + i need the column for A,B,C on the left to be with less width compared to others (e.g., each coloumn with different width or auto-fit text). Again, thank you so much!
    – MWH
    Mar 30, 2017 at 10:35
  • The font size is thz default (10pt). If you want a smmaller font size you can add \small or \footnotesize right after \begin{table}. And yes, you can change the first column specifier to l in the place of X . Your post had three equal width columns, so I tried to reproduce the general look. If the contents of the first column requires entries on several lines, you also can choose a fixed width with p{nn cm}. Or, last possibility, if you want the first column to have, say, half the width of the other two, replace {XXX}with {>{\hsize=0.6\hsize}X *{2}>{\hsize=1.2\hsize}X{}}.
    – Bernard
    Mar 30, 2017 at 15:30
  • Thanks. I still did not get it. I would be thankful if you edit it to match the syntax the table in the post (i updated the look of the table)
    – MWH
    Mar 31, 2017 at 17:56
  • @MWH: I've added another (simpler) code so that the first column has width half the width of the other columns. Is it more like you want?
    – Bernard
    Apr 1, 2017 at 0:12
  • Thanks for your effort. It is perfect. i just got grey lines around the table and would prefer black table borders rather than red. I would prefer the style of the first one with just adjusting the width. Thanks anyway!
    – MWH
    Apr 3, 2017 at 21:07
2

To let LaTeX calculate the column width automatically, you need something like the tabularx package that provides columns of type X.

To have cells that span several rows, you can use the multirow package.

To get nicer, more professional looking tables, use the booktabs package.

To get compact itemized lists in the table, either use the paralist package

\usepackage{paralist}
...
\begin{compactitem}
\item ...
\item ...
...
\end{compactitem}

or the enumitem package:

\usepackage{enumitem}
\newenvironment{compactitem}%
  {\begin{itemize}[nosep]}%
  {\end{itemize}}
...
\begin{compactitem}
\item ...
\item ...
...
\end{compactitem}

Here is the sketch of the table in the traditional look (= excessive use of vertical lines), with the code below it. After that you find a version with booktabs, without vertical lines.

enter image description here

\documentclass{article}
\usepackage{tabularx,multirow}
\usepackage{enumitem}
\newenvironment{compactitem}%
  {\begin{itemize}[nosep]}%
  {\end{itemize}}
%\usepackage{paralist}
\newcommand\sometext
  {sdf dfsdf sdfsdf s df sdfsdf sdfsdsdf sdf s.
   sdf dfsdf sdfsdf s df sdfsdf sdfsdsdf sdf s.
  }
\begin{document}
\noindent
\begin{tabularx}{1.0\linewidth}{|X|X|X|}
\hline
  \multicolumn{2}{|c|}{XXX}
      & \multicolumn{1}{c|}{YYY}
\\\hline
  \multirow{3}{=}{A \sometext \sometext \sometext}
    & A1 \sometext
      & \begin{compactitem}
        \item A1.1
        \item A1.2
        \item A1.3
        \end{compactitem}
\\\cline{2-3}
    & A2 \sometext
      & \begin{compactitem}
        \item A2
        \end{compactitem}
\\\cline{2-3}
    & A3 \sometext
      & \begin{compactitem}
        \item A3
        \end{compactitem}
\\\hline
  \multirow{2}{=}{B \sometext \sometext \sometext}
    & B1 \sometext
      & \begin{compactitem}
        \item B1.1
        \item B1.2
        \item B1.3
        \end{compactitem}
\\\cline{2-3}
    & B2 \sometext
      & \begin{compactitem}
        \item B2.1
        \item B2.2
        \end{compactitem}
\\[11.5ex]\hline
\end{tabularx}
\end{document}

enter image description here

\documentclass{article}
\usepackage{tabularx,multirow,booktabs}
\usepackage{enumitem}
\newenvironment{compactitem}%
  {\begin{itemize}[nosep]}%
  {\end{itemize}}
%\usepackage{paralist}
\newcommand\sometext
  {sdf dfsdf sdfsdf s df sdfsdf sdfsdsdf sdf s.
   sdf dfsdf sdfsdf s df sdfsdf sdfsdsdf sdf s.
  }
\begin{document}

\noindent
\begin{tabularx}{1.0\linewidth}{XXX}
\toprule
  \multicolumn{2}{c}{XXX}
      & \multicolumn{1}{c}{YYY}
\\\midrule
  \multirow{3}{=}{A \sometext \sometext \sometext}
    & A1 \sometext
      & \begin{compactitem}
        \item A1.1
        \item A1.2
        \item A1.3
        \end{compactitem}
\\\cmidrule{2-3}
    & A2 \sometext
      & \begin{compactitem}
        \item A2
        \end{compactitem}
\\\cmidrule{2-3}
    & A3 \sometext
      & \begin{compactitem}
        \item A3
        \end{compactitem}
\\\midrule
  \multirow{2}{\linewidth}{B \sometext \sometext \sometext}
    & B1 \sometext
      & \begin{compactitem}
        \item B1.1
        \item B1.2
        \item B1.3
        \end{compactitem}
\\\cmidrule{2-3}
    & B2 \sometext
      & \begin{compactitem}
        \item B2.1
        \item B2.2
        \end{compactitem}
\\[11.5ex]\bottomrule
\end{tabularx}
\end{document}
1
  • Thanks a lot gernot! will look at the code and play with it
    – MWH
    Mar 30, 2017 at 10:40
2

You can start with something like this:

\documentclass[12pt,a4paper]{article}
\usepackage{enumitem, tabularx}
\begin{document}

\setlist[itemize]{leftmargin = *, itemsep=-3pt, 
                  before=\vspace*{-\dimexpr\baselineskip+\partopsep}, 
                  after=\vspace*{\dimexpr-\baselineskip+\partopsep}}

\begin{tabularx}{\linewidth}{|*3{X|}}
\hline
\multicolumn{2}{|c|}{XXX} & YYY   \\ \hline
  A   & A1      & \begin{itemize}
                      \item A1.1
                      \item A1.2
                      \item A1.3
                  \end{itemize}   \\ \cline{2-3} 
      & A2      & A2              \\ \cline{2-3} 
      & A3      & A3              \\ \hline
  B   & B1      & \begin{itemize}
                       \item B1.1
                       \item B1.2
                       \item B1.3
                  \end{itemize}   \\ \cline{2-3} 
      & B2      & ? B2.1          \\ \cline{2-3} 
      & B3      & ? B3.1          \\ \cline{2-3} 
      & B4      & ? B4            \\ \cline{2-3} 
      & B5      & ? B5            \\ \cline{2-3} 
      & B6      & ? B6            \\ \hline
  C   & Cl      & Cl.l            \\
      &         &                 \\ \cline{2-3} 
      & C2      & C2.1            \\
      &         &                 \\ \hline
\end{tabularx}

\end{document}

enter image description here

0

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.