5

After read some examples of matrix of nodes, i wrote this MWE:

\documentclass{article}

\usepackage{tikz}\usetikzlibrary{matrix}
\usepackage{booktabs} 

\begin{document}

See the Table \ref{fig:cen2}.

\begin{table}[htb!]
    \caption{My very very very very very long useless caption}
    \label{fig:cen2}
    \begin{tikzpicture}
        \matrix (m) [matrix of nodes,
        nodes in empty cells,
        toprule/.style={%
            execute at end cell={%
                \draw [line cap=rect,#1] (\tikzmatrixname-\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn.north west) -- (\tikzmatrixname-\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn.north east);%
            }
        },
    bottomrule/.style={%
        execute at end cell={%
            \draw [line cap=rect,#1] (\tikzmatrixname-\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn.south west) -- (\tikzmatrixname-\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn.south east);%
            }
        },
    midrule/.style={%
        execute at end cell={%
            \draw  (\tikzmatrixname-\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn.south east)--(\tikzmatrixname-\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn.south east);%
            }

        },
%
        anchor=base,
        row sep    = -\pgflinewidth,
        column sep = -\pgflinewidth,
%
        row 1/.style={nodes={font=\bfseries, align = center}, anchor = south},%toprule = thick
        row 2/.style={anchor = south},
        row 3/.style={anchor = south},
        row 4/.style={anchor = south},
        row 5/.style={anchor = south},
%       
        column 1/.style={nodes={ text width=2.5cm,align=left, font = \small}},
        column 2/.style={nodes={ text width=4.0cm,align=left, font = \small}},
        column 3/.style={nodes={ text width=4.0cm,align=left, font = \small}},
        column 4/.style={nodes={ text width=4.0cm,align=left, font = \small}}%rectangle,draw,
        ] at (0,0)
%
        {%
            Variables & Hypothesis 1 & Hypothesis 2 & Hypothesis 3 \\
            Territorial unit &
            Final destination for receiving waste generated in the municipality &
            Final destination for receiving waste generated in the municipality &
            Final destination to receive the waste generated in the municipality \\
            Index of coverage &
            Maintenance of the urban coverage index, with focus on short term &
            Maintenance of the urban coverage index, focusing on medium term &
            Maintenance of the urban coverage index, with a long-term focus \\
            Garbage Collection with Recovery of Recyclable garbage &
            Increase in the Recovery of Recyclable garbage index in the short term &
            Moderate increase in the recovery of recyclable garbage index with focus on medium term &
            Expectation of implementation of long-term selective collection \\
            Composting of organic waste &
            Adhesion and massive investment in compost disposal final solid organic waste &
            Adhesion and moderate investment in composting final disposal of organic solid waste &
            Investment in composting organic long-term solid waste \\
        };

        \draw [-, very thick] (m-1-1.north west) -- (m-1-4.north east);%linha 1
        \draw [-, thick]      (m-1-4.south east) -- (m-1-1.south west);%linha 2
        \draw [-, thin]       (m-2-1.south west) -- (m-2-4.south east);%linha 3
        \draw [-, thin]       (m-3-1.south west) -- (m-3-4.south east);%linha 4
        \draw [-, thin]       (m-4-1.south west) -- (m-4-4.south east);%linha 5
        \draw [-, thick]      (m-5-1.south west) -- (m-5-4.south east);%linha 6
%       
        \draw[->, color = green,  ultra thick, dashed] (m-2-2.mid) -- ([yshift = -5mm]m-5-2.south);
        \draw[->, color = orange, ultra thick, dashed] (m-2-3.mid) -- ([yshift = -5mm]m-5-3.south);
        \draw[->, color = red,    ultra thick, dashed] (m-2-4.mid) -- ([yshift = -5mm]m-5-4.south);
%       
        \node [below, yshift = -5mm] at (m-5-2.south) {\textcolor{green} {\bfseries Scenario 1}};
        \node [below, yshift = -5mm] at (m-5-3.south) {\textcolor{orange}{\bfseries Scenario 2}};
        \node [below, yshift = -5mm] at (m-5-4.south) {\textcolor{red}   {\bfseries Scenario 3}};
    \end{tikzpicture}
\end{table}

\end{document}

But the matrix has crooked lines. How to adjust the lines in horizontal?

Matrix with crooked lines

  • 2
    I'm having trouble understanding the question. Which lines do you want to "repair"? The colored ones, in which you manually added the yshift? –  Feb 14 '18 at 18:28
  • the horizontal lines. the spacing between the lines in cell "Hypothesis 3", is greater than in the cell "Variables". – leonardo cesar Feb 14 '18 at 18:34
  • all horizontal line are "horizontal" and all vertical are "vertical" ... – Zarko Feb 14 '18 at 18:34
  • 1
    this is problem of cell content. difference is in letters y and p, which are not present inVariables. you should redefine nodes size (add text depth=0.25ex for example) in the first row. similarly effect you have in other cells too (see first column) – Zarko Feb 14 '18 at 18:37

3 Answers3

7

i would set table on different way ...

  • use standard tabularx table environment
  • for vertical dashed lines use tikzmark library
  • align cells content on top
  • use raged2e package for nicer align text in cells
  • use booktabs for horizontal rules, from whic i would use only three
  • use makecell for column headers and "labels" at bottom of table

considering above the table code become much shorter, simple and clear (to my opinion). of course, to obtain final table you need to compile the code at least twice.

enter image description here

\documentclass{article}
\usepackage{geometry}

\usepackage{tikz}
\usetikzlibrary{arrows.meta, calc, tikzmark}
\usepackage{booktabs, makecell, tabularx}
\renewcommand\theadfont{\normalsize\bfseries}
\renewcommand\theadgape{}
\usepackage{ragged2e}
\usepackage[labelfont=bf,
            skip=1ex]{caption}

\begin{document}

See the Table \ref{fig:cen2}.
    \begin{table}[htb!]
\caption{My very very very very very long useless caption}
    \label{fig:cen2}
\small
\begin{tabularx}{\linewidth}{>{\RaggedRight}p{25mm} *{3}{>{\RaggedRight}X}}
        \toprule
    \thead{Variables}   &
    \tikzmark{As}\thead{Hypothesis 1}\tikzmark{Ae}  &
    \tikzmark{Bs}\thead{Hypothesis 2}\tikzmark{Be}  &
    \tikzmark{Cs}\thead{Hypothesis 3}\tikzmark{Ce}                      \\
        \midrule
    Territorial unit &
    Final destination for receiving waste generated in the municipality &
    Final destination for receiving waste generated in the municipality &
    Final destination to receive the waste generated in the municipality \\
        \addlinespace
    Index of coverage &
    Maintenance of the urban coverage index, with focus on short term &
    Maintenance of the urban coverage index, focusing on medium term &
    Maintenance of the urban coverage index, with a long-term focus \\
        \addlinespace
    Garbage Collection with Recovery of Recyclable garbage &
    Increase in the Recovery of Recyclable garbage index in the short term &
    Moderate increase in the recovery of recyclable garbage index with focus on medium term &
    Expectation of implementation of long-term selective collection \\
        \addlinespace
    Composting of organic waste &
    Adhesion and massive investment in compost disposal final solid organic waste &
    Adhesion and moderate investment in composting final disposal of organic solid waste &
    Investment in composting organic long-term solid waste \\
        \midrule[\heavyrulewidth]
        \addlinespace
        &
    \tikzmark{AAs}\thead{\textcolor{green}{Scenario 1}}\tikzmark{AAe} &
    \tikzmark{BBs}\thead{\textcolor{orange}{Scenario 2}}\tikzmark{BBe}&
    \tikzmark{CCs}\thead{\textcolor{red}{Scenario 3}}\tikzmark{CCe}
    \end{tabularx}
\begin{tikzpicture}[overlay, remember picture,
line/.style = {draw=#1, line width=1mm, densely dashed, -Straight Barb,
               semitransparent, shorten <=5mm, shorten >=3mm}
                    ]
\draw[line=green]   ($(pic cs:As)!0.5!(pic cs:Ae)$) -- ($(pic cs:AAs)!0.5!(pic cs:AAe)$);
\draw[line=orange]  ($(pic cs:Bs)!0.5!(pic cs:Be)$) -- ($(pic cs:BBs)!0.5!(pic cs:BBe)$);
\draw[line=red]     ($(pic cs:Cs)!0.5!(pic cs:Ce)$) -- ($(pic cs:CCs)!0.5!(pic cs:CCe)$);
\end{tikzpicture}
    \end{table}
\end{document}
Zarko
  • 296,517
  • You did not really hibernate, did you? (+1) –  Feb 14 '18 at 20:16
  • this solution besides correcting the horizontal lines, has a better text alignment. – leonardo cesar Feb 14 '18 at 23:08
  • 1
    @leonardocesar, let me emphasize again. horizontal lines in any solution in answers as well in your question are correct (strict horizontal). Differences are in nodes content position. in your example are centered in node. this means, that their position depends on content. for example in nodes with one letter, position of a differ from position p and position of k since their heights and depths are different. in my answer this problem not exist. all text line have the same baseline. this means that in all cells all letters are aligned to common baseline. – Zarko Feb 15 '18 at 07:46
5

You can fix it by adding \strut.

\documentclass{article}

\usepackage{tikz}\usetikzlibrary{matrix}
\usepackage{booktabs} 

\begin{document}

See the Table \ref{fig:cen2}.

\begin{table}[htb!]
    \caption{My very very very very very long useless caption}
    \label{fig:cen2}
    \begin{tikzpicture}
        \matrix (m) [matrix of nodes,
        nodes in empty cells,
        toprule/.style={%
            execute at end cell={%
                \draw [line cap=rect,#1] (\tikzmatrixname-\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn.north west) -- (\tikzmatrixname-\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn.north east);%
            }
        },
    bottomrule/.style={%
        execute at end cell={%
            \draw [line cap=rect,#1] (\tikzmatrixname-\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn.south west) -- (\tikzmatrixname-\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn.south east);%
            }
        },
    midrule/.style={%
        execute at end cell={%
            \draw  (\tikzmatrixname-\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn.south east)--(\tikzmatrixname-\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn.south east);%
            }

        },
%
        baseline=base,
        row sep    = -\pgflinewidth,
        column sep = -\pgflinewidth,
%
        row 1/.style={nodes={font=\bfseries\strut, align = center}, anchor = south},%toprule = thick
        row 2/.style={anchor = south},
        row 3/.style={anchor = south},
        row 4/.style={anchor = south},
        row 5/.style={anchor = south},
%       
        column 1/.style={nodes={text width=2.5cm,align=left, font = \small\strut}},
        column 2/.style={nodes={text width=4.0cm,align=left, font = \small\strut}},
        column 3/.style={nodes={text width=4.0cm,align=left, font = \small\strut}},
        column 4/.style={nodes={text width=4.0cm,align=left, font = \small\strut}}%rectangle,draw,
        ] at (0,0)
%
        {%
            Variables & Hypothesis 1 & Hypothesis 2 &
            Hypothesis 3 \\
            Territorial unit &
            Final destination for receiving waste generated in the municipality &
            Final destination for receiving waste generated in the municipality &
            Final destination to receive the waste generated in the municipality \\
            Index of coverage &
            Maintenance of the urban coverage index, with focus on short term &
            Maintenance of the urban coverage index, focusing on medium term &
            Maintenance of the urban coverage index, with a long-term focus \\
            Garbage Collection with Recovery of Recyclable garbage &
            Increase in the Recovery of Recyclable garbage index in the short term &
            Moderate increase in the recovery of recyclable garbage index with focus on medium term &
            Expectation of implementation of long-term selective collection \\
            Composting of organic waste &
            Adhesion and massive investment in compost disposal final solid organic waste &
            Adhesion and moderate investment in composting final disposal of organic solid waste &
            Investment in composting organic long-term solid waste \\
        };

        \draw [-, very thick] (m-1-1.north west) -- (m-1-4.north east);%linha 1
        \draw [-, thick]      (m-1-4.south east) -- (m-1-1.south west);%linha 2
        \draw [-, thin]       (m-2-1.south west) -- (m-2-4.south east);%linha 3
        \draw [-, thin]       (m-3-1.south west) -- (m-3-4.south east);%linha 4
        \draw [-, thin]       (m-4-1.south west) -- (m-4-4.south east);%linha 5
        \draw [-, thick]      (m-5-1.south west) -- (m-5-4.south east);%linha 6
%       
        \draw[->, color = green,  ultra thick, dashed] (m-2-2.mid) -- ([yshift = -5mm]m-5-2.south);
        \draw[->, color = orange, ultra thick, dashed] (m-2-3.mid) -- ([yshift = -5mm]m-5-3.south);
        \draw[->, color = red,    ultra thick, dashed] (m-2-4.mid) -- ([yshift = -5mm]m-5-4.south);
%       
        \node [below, yshift = -5mm] at (m-5-2.south) {\textcolor{green} {\bfseries Scenario 1}};
        \node [below, yshift = -5mm] at (m-5-3.south) {\textcolor{orange}{\bfseries Scenario 2}};
        \node [below, yshift = -5mm] at (m-5-4.south) {\textcolor{red}   {\bfseries Scenario 3}};
    \end{tikzpicture}
\end{table}

\end{document}

\begin{table}[htb!]
    \caption{My very very very very very long useless caption}
    \label{fig:cen2}
    \begin{tikzpicture}
        \matrix (m) [matrix of nodes,
        nodes in empty cells,
        toprule/.style={%
            execute at end cell={%
                \draw [line cap=rect,#1] (\tikzmatrixname-\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn.north west) -- (\tikzmatrixname-\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn.north east);%
            }
        },
    bottomrule/.style={%
        execute at end cell={%
            \draw [line cap=rect,#1] (\tikzmatrixname-\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn.south west) -- (\tikzmatrixname-\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn.south east);%
            }
        },
    midrule/.style={%
        execute at end cell={%
            \draw  (\tikzmatrixname-\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn.south east)--(\tikzmatrixname-\the\pgfmatrixcurrentrow-\the\pgfmatrixcurrentcolumn.south east);%
            }

        },
%
        baseline=base,
        row sep    = -\pgflinewidth,
        column sep = -\pgflinewidth,
%
        row 1/.style={nodes={font=\bfseries\strut, align = center}, anchor = south},%toprule = thick
        row 2/.style={anchor = south},
        row 3/.style={anchor = south},
        row 4/.style={anchor = south},
        row 5/.style={anchor = south},
%       
        column 1/.style={nodes={ text width=2.5cm,align=left, font = \small\strut}},
        column 2/.style={nodes={ text width=4.0cm,align=left, font = \small\strut}},
        column 3/.style={nodes={ text width=4.0cm,align=left, font = \small\strut}},
        column 4/.style={nodes={ text width=4.0cm,align=left, font = \small\strut}}%rectangle,draw,
        ] at (0,0)
%
        {%
            Variables & Hypothesis 1 & Hypothesis 2 &
            Hypothesis 3 \\
            Territorial unit &
            Final destination for receiving waste generated in the municipality &
            Final destination for receiving waste generated in the municipality &
            Final destination to receive the waste generated in the municipality \\
            Index of coverage &
            Maintenance of the urban coverage index, with focus on short term &
            Maintenance of the urban coverage index, focusing on medium term &
            Maintenance of the urban coverage index, with a long-term focus \\
            Garbage Collection with Recovery of Recyclable garbage &
            Increase in the Recovery of Recyclable garbage index in the short term &
            Moderate increase in the recovery of recyclable garbage index with focus on medium term &
            Expectation of implementation of long-term selective collection \\
            Composting of organic waste &
            Adhesion and massive investment in compost disposal final solid organic waste &
            Adhesion and moderate investment in composting final disposal of organic solid waste &
            Investment in composting organic long-term solid waste \\
        };

        \draw [-, very thick] (m-1-1.north west) -- (m-1-4.north east);%linha 1
        \draw [-, thick]      (m-1-4.south east) -- (m-1-1.south west);%linha 2
        \draw [-, thin]       (m-2-1.south west) -- (m-2-4.south east);%linha 3
        \draw [-, thin]       (m-3-1.south west) -- (m-3-4.south east);%linha 4
        \draw [-, thin]       (m-4-1.south west) -- (m-4-4.south east);%linha 5
        \draw [-, thick]      (m-5-1.south west) -- (m-5-4.south east);%linha 6
%       
        \draw[->, color = green,  ultra thick, dashed] (m-2-2.mid) -- ([yshift = -5mm]m-5-2.south);
        \draw[->, color = orange, ultra thick, dashed] (m-2-3.mid) -- ([yshift = -5mm]m-5-3.south);
        \draw[->, color = red,    ultra thick, dashed] (m-2-4.mid) -- ([yshift = -5mm]m-5-4.south);
%       
        \node [below, yshift = -5mm] at (m-5-2.south) {\textcolor{green} {\bfseries Scenario 1}};
        \node [below, yshift = -5mm] at (m-5-3.south) {\textcolor{orange}{\bfseries Scenario 2}};
        \node [below, yshift = -5mm] at (m-5-4.south) {\textcolor{red}   {\bfseries Scenario 3}};
    \end{tikzpicture}
\end{table}

\end{document}

enter image description here

But I must admit that I am also lost (like hibernating Zarko;-) because you superimpose column-wise and row-wise directives. Before fixing the lower cells, I'd need to know which alignment you're aiming up.

  • nice!! now the horizontal lines are really horizontal. thankyou – leonardo cesar Feb 14 '18 at 18:48
  • @CarLaTeX Grazie, fixed my bug ;-) –  Feb 14 '18 at 18:51
  • @leonardocesar, your conclusion is wrong. lines are not changed, changed is position of first cell content (it is slightly raised, as it has letter p in word). this problem still persist for example in the second and the last row. – Zarko Feb 14 '18 at 18:51
  • @Zarko, so should I add \ strut on all other lines? – leonardo cesar Feb 14 '18 at 18:57
  • is not so simple. i need to check this. now i'm still lost in your code :-(. when i will understand how each of your definitions influence on table cells, i will probably write own answer (it seems that i hibernate instead of marmot :-). ) – Zarko Feb 14 '18 at 19:04
  • @marmot, finally i wake up (completely) :-). nice answer (+1), but for my test for desired result the op's approach is "overkill" ... and has many side underside effects, which is difficult to solve with tikz matrix. so i decide to offer different way to write this matrix. see my answer below. – Zarko Feb 14 '18 at 20:12
  • @Zarko I agree with you, but ultimately I guess there will be some nice TikZ tables, I guess. Then your nice trick here will be much easier to accomplish. –  Feb 14 '18 at 20:18
4

I was going to suggest the matrixcells package, but then in searching on this site for the original question that sparked that package, I found that someone had fixed some of the issues with that package so I'm recommending using the matrix.skeleton package (from Matrixcells problem with the y-axis only) instead.

I've removed a bit of unused code.

\documentclass{article}
%\url{https://tex.stackexchange.com/q/415345/86}

\usepackage{tikz}\usetikzlibrary{matrix}
\usepackage{booktabs}
\usetikzlibrary{matrix.skeleton}

\begin{document}

See the Table \ref{fig:cen2}.

\begin{table}[htb!]
    \caption{My very very very very very long useless caption}
    \label{fig:cen2}
    \begin{tikzpicture}
\matrix (m) [matrix of nodes,
  nodes in empty cells,
  label skeleton,
%
        anchor=base,
        row sep    = -\pgflinewidth,
        column sep = -\pgflinewidth,
%
        row 1/.style={nodes={font=\bfseries, align = center}, anchor = south},%toprule = thick
        row 2/.style={anchor = south},
        row 3/.style={anchor = south},
        row 4/.style={anchor = south},
        row 5/.style={anchor = south},
%       
        column 1/.style={nodes={ text width=2.5cm,align=left, font = \small}},
        column 2/.style={nodes={ text width=4.0cm,align=left, font = \small}},
        column 3/.style={nodes={ text width=4.0cm,align=left, font = \small}},
        column 4/.style={nodes={ text width=4.0cm,align=left, font = \small}}%rectangle,draw,
        ] at (0,0)
%
        {%
            Variables & Hypothesis 1 & Hypothesis 2 & Hypothesis 3 \\
            Territorial unit &
            Final destination for receiving waste generated in the municipality &
            Final destination for receiving waste generated in the municipality &
            Final destination to receive the waste generated in the municipality \\
            Index of coverage &
            Maintenance of the urban coverage index, with focus on short term &
            Maintenance of the urban coverage index, focusing on medium term &
            Maintenance of the urban coverage index, with a long-term focus \\
            Garbage Collection with Recovery of Recyclable garbage &
            Increase in the Recovery of Recyclable garbage index in the short term &
            Moderate increase in the recovery of recyclable garbage index with focus on medium term &
            Expectation of implementation of long-term selective collection \\
            Composting of organic waste &
            Adhesion and massive investment in compost disposal final solid organic waste &
            Adhesion and moderate investment in composting final disposal of organic solid waste &
            Investment in composting organic long-term solid waste \\
        };

        \draw [-, very thick] (m-row-1.north west) -- (m-row-1.north east);%linha 1
        \draw [-, thick]      (m-row-2.north west) -- (m-row-2.north east);%linha 2
        \draw [-, thin]       (m-row-3.north west) -- (m-row-3.north east);%linha 3
        \draw [-, thin]       (m-row-4.north west) -- (m-row-4.north east);%linha 4
        \draw [-, thin]       (m-row-5.north west) -- (m-row-5.north east);%linha 5
        \draw [-, thick]      (m-row-5.south west) -- (m-row-5.south east);%linha 6
%       
        \draw[->, color = green,  ultra thick, dashed] (m-2-2.mid) -- ([yshift = -5mm]m-5-2.south);
        \draw[->, color = orange, ultra thick, dashed] (m-2-3.mid) -- ([yshift = -5mm]m-5-3.south);
        \draw[->, color = red,    ultra thick, dashed] (m-2-4.mid) -- ([yshift = -5mm]m-5-4.south);
%       
        \node [below, yshift = -5mm] at (m-5-2.south) {\textcolor{green} {\bfseries Scenario 1}};
        \node [below, yshift = -5mm] at (m-5-3.south) {\textcolor{orange}{\bfseries Scenario 2}};
        \node [below, yshift = -5mm] at (m-5-4.south) {\textcolor{red}   {\bfseries Scenario 3}};
    \end{tikzpicture}
\end{table}

\end{document}

Straight lines in a matrix

Andrew Stacey
  • 153,724
  • 43
  • 389
  • 751
  • Loop Space, but "Variables" is still misaligned... – CarLaTeX Feb 14 '18 at 21:41
  • 1
    @CarLaTeX I was answering the question in the question, I didn't look at the comments. Fixing that is simple: change the anchor to base instead of south. – Andrew Stacey Feb 14 '18 at 21:47
  • nice solution, but the problem is installation of matrix.skeleton library. it is not a part of tikz package (what is the pity) nor formatted as package which can be simply installed for example with package manager in miktex. do you maybe know, if it will be in some near future included to tikz? – Zarko Feb 14 '18 at 21:54
  • @Zarko Given that the last update to the package was four years ago, I don't think it's making its way to CTAN any time soon. But it is simple to install: download the two library files from github and you're done. – Andrew Stacey Feb 14 '18 at 21:59