0

I need to write a table like the image below in Latex. enter image description here

This table represents the data structure of a message, which some specific data of the message and other fields that are inserted into two datastructure (section A and section B). Each section has also an other nested subsection (respectively subsection A.1 and subsection B.1).

Each row contains three column: Field, Type, Description.

I began writing the table without the nested section:

\begin{longtable}{p{5cm}p{1cm}p{6cm}}

% didascalia ed etichetta
\caption{caption name}
\label{tab:name of table} \\

% intestazione iniziale
\toprule
\textbf{Field} &\textbf{Type} &\textbf{Description}\\

\midrule
\endfirsthead
% intestazione normale
\multicolumn{2}{l}{\footnotesize\itshape\tablename~\thetable:
continued from previous page} \\
\toprule
\textbf{Field} &\textbf{Type} &\textbf{Description}\\

\midrule
\endhead
% piede normale
\midrule
\multicolumn{2}{r}{\footnotesize\itshape\tablename~\thetable:
Continued on next page} \\
\endfoot
% piede finale
\bottomrule
\multicolumn{2}{r}{\footnotesize\itshape\tablename~\thetable:
si conclude dalla pagina precedente} \\
\endlastfoot

% corpo della tabella

field\_name &   String  &   description of some rows    \\

\end{longtable}

My questions are:

  1. What is the best way to represent the nested sections into a Latex Table?
  2. I wrote the table in the image by MS Excel: section A and setion B are done with the union of cells of that row. How can I obtain a similar result in Latex?

Thank you in advance for your help and collaboration!

1

If you don't insist on a real table, it can be done quite easily with TikZ. It has the mayor downside that it will fail with multiline text, but you can specify an optional parameter to give a line some extra space. It does not break across pages.

Code

\documentclass{scrartcl}
\usepackage[margin=10mm]{geometry}
\usepackage{tikz}
\usetikzlibrary{calc}

\newcommand{\ypos}{0}
\newcommand{\ysep}{0.6}
\newcommand{\widthA}{5}
\newcommand{\widthB}{3.5}
\newcommand{\widthC}{8}
\newcommand{\colSep}{0.25}
\newcommand{\boxWidth}{1}

\newcommand{\Line}[4][0]%
{   \node[below right, text width=\widthA*1cm, align=left, inner sep=1mm] at (0,\ypos) {#2};
    \node[below right, text width=\widthB*1cm, align=left, inner sep=1mm] at (\widthA+\colSep,\ypos) {#3};
    \node[below right, text width=\widthC*1cm, align=left, inner sep=1mm] at (\widthA+\widthB+2*\colSep,\ypos) {#4};
    \pgfmathparse{\ypos-\ysep-#1}
    \xdef\ypos{\pgfmathresult}
}

\newcommand{\Heading}[2]%
{   \node[below right, text width={(\widthA+\widthB+\widthC+2*\colSep)*1cm-2.5mm}, align=left, fill=#1, inner sep=1.25mm] at (0,\ypos) {#2};
    \pgfmathparse{\ypos-\ysep}
    \xdef\ypos{\pgfmathresult}
}

\newcommand{\StartColorBox}[1]%
% color
{   \xdef\BoxColor{#1}
    \xdef\BoxStart{\ypos}
    \pgfmathparse{\ypos-\ysep}
    \xdef\ypos{\pgfmathresult}
}

\newcommand{\EndColorBox}%
% color
{   \fill[\BoxColor, yshift=-\ysep*1cm](0,\BoxStart) -| ++ (\widthA+\widthB+\widthC+2*\colSep,\ysep) -| ($(0,\ypos)+(-\boxWidth,0)$) -| ++ (\boxWidth+\widthA+\widthB+\widthC+2*\colSep,\ysep) -| cycle;
    \pgfmathparse{\ypos-\ysep}
    \xdef\ypos{\pgfmathresult}
}

\begin{document}

\begin{tikzpicture}
    \Line{field\_name}{type}{description}
    \Line{field\_name}{type}{description}
    \StartColorBox{blue!50!gray}
        \Heading{green!50!gray!50}{SECTION X: Lorem ipsum}
            \Line{field\_name}{type}{description}
            \Line{field\_name}{type}{description}
            \Line{field\_name}{type}{description}
        \Heading{orange!50!gray!50}{SECTION X: Lorem ipsum}
            \Line{field\_name}{type}{description}
            \Line{field\_name}{type}{description}
    \EndColorBox
    \StartColorBox{red!50!gray}
        \Heading{green!50!gray!50}{SECTION X: Lorem ipsum}
            \Line[0.45]{field\_name}{type}{description description description description description}
            \Line{field\_name}{type}{description}
            \Line{field\_name}{type}{description}
            \Line{field\_name}{type}{description}
            \Line{field\_name}{type}{description}
            \Line{field\_name}{type}{description}
        \Heading{orange!50!gray!50}{SECTION X: Lorem ipsum}
            \Line{field\_name}{type}{description}
            \Line{field\_name}{type}{description}
            \Line{field\_name}{type}{description}
            \Line{field\_name}{type}{description}
    \EndColorBox
\end{tikzpicture}

\end{document}

Output

enter image description here

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.