TransWikia.com

Adding nodes and lines to a table

TeX - LaTeX Asked on March 10, 2021

I am trying to draw a table like the one in the picture below. I’ve been working with tables and tikz but never combined both to draw a table with nodes in it.

Desired Table

What would be the best approach to draw such a table?

I created a minimum working example, which still has some issues.

  • I don’t know how to control the alignment of the lines inside the cells
  • The circles are missing
  • I did not include the horizontal and vertical lines as I also don’t know how to align them.

Any help is appreciated. I can also use other packages if necessary. This was just my first idea.

Code:

documentclass{article}
usepackage{array,multirow,graphicx}
 usepackage{tikz}
usetikzlibrary{tikzmark}
 begin{document}
 begin{table}[h]
 centering
 begin{tabular}{c|l|cccccc}
 Kategorie& multicolumn{1}{c|}{Einflussgroesse} & multicolumn{6}{c}{Auspraegung in Fallstudie} 
 &&&1&2&3&4&
 hline
 multirow{4}{*}{rotatebox[origin=c]{90}{parbox[c]{1.5cm}{centering Markt-bezogene }}} & text &gering&&tikzmark{1}&&&hoch
 & text &gering&&tikzmark{2}&&&hoch
 & text &gering&&&tikzmark{3}&&hoch
  & text &gering&&&tikzmark{4}&&hoch
 end{tabular}
 begin{tikzpicture}[overlay, remember picture]
    draw [-] ({pic cs:1}) -- ({pic cs:2});
    draw [-] ({pic cs:2}) -- ({pic cs:3});
    draw [-] ({pic cs:3}) -- ({pic cs:4});
  end{tikzpicture}
 end{table}
 end{document}

Which will give:
Output

One Answer

If you put the table inside a tikz matrix of nodes (see chapter 20 of the tikz/pgf manual), then you will have the full power of tikz at your disposal. To all intents and purposes a matrix of nodes looks like a "normal" LaTeX matrix except:

  • It sits inside a tikzpicture environment.
  • You can style the rows., columns, nodes ... in the usual tikz fashion
  • It is straightforward to draw lines etc between the entries of the matrix.

Here the result of my experimenting with your MWE:

enter image description here

...and here is the code that produced this:

documentclass{article}
usepackage{tikz}
usetikzlibrary{calc,matrix}

newcommandhochscale[1]{%hochscale{a,b,c,d,...}
  foreach r in {1,2,3,4} {% put numbers under heading
     node at ($ (M-1-3.west)+(r,-0.4) $) {r};
  }
  % now draw scale in each row
  foreach hoch [count=r from 2, remember=r as last] in {#1} {
    node[anchor=west] at ($ (M-r-3.west)+(-0.8,0) $) {gering};
    node[anchor=east] at ($ (M-r-3.west)+(5.5,0) $)    {hoch};
    draw (M-r-3.west)+(0.5,0) -- ++(4.5,0);
    node[mypoint] (hochr) at ($ (M-r-3.west)+(hoch,0) $) {};
    ifnumr>2% draw the lines between scales starting from row 3
      draw[blue](hochlast)--(hochr);
    fi
  }
  % add the dashed line
  draw[dashed](M-2-3.west)+(2.5,0.5)--($ (M-last-3.west)+(2.5,-0.5) $);
}

begin{document}
  begin{tikzpicture}[mypoint/.style={circle, radius=0.5mm, fill=blue}]
    matrix (M)[matrix of nodes, nodes in empty cells,
                row sep=8mm, column sep=6mm,
                row 1/.style = {anchor=center, font=largebfseries, blue,
                                minimum height=12mm},% to allow for numbers
                column 1/.style = {text width=15mm},
                column 2/.style = {text width=60mm},
                column 3/.style = {text width=60mm},
                ]{
     Kategorie & Einflussgroesse &  Auspraegung in Fallstudie
               & text 1 & 
               & text 2 & 
               & text 3 & 
               & text 4 & 
               & text 5 & 
     };
     draw[blue, thick](M-1-1.south west) -- (M-1-3.south east);
     node[rotate=90, anchor=east] at (M-3-1) {Markt-bezogene};
     hochscale{1,2,3,2,4}% add scales in column 3
  end{tikzpicture}

end{document}

A few comments:

  • The (M) after the matrix command says that the nodes of the matrix can be referred to as (M-1-1), (M-1-2), ... and more generally (M-row-col), where row and col are any row and column indices. You can replace M with anything you like.
  • The hochscale command prints the scales in column 3. It accepts a comma separated list of the "scale values" after which it does the rest. This command is basically a foreach statement running over the values. It uses the (M-r-3) syntax to refer to the node in row r and column 3. The node positions of the form ($ .... $) use the tikz calc library.
  • I have added some random styling to row 1. In general the styling is random and you should change to suit your needs. (In particular, there is currently too much blue!)
  • In practice, you will probably need to change the width of the columns so that it better suits your text. In particular, you will probably need to shrink the scales in column 3 because otherwise I do not think that your text will fit.
  • As explained in the booktabs manual, it is usually better to avoid vertical lines in tables.
  • A better example of the possibilities of styling is perhaps:

enter image description here

which is given by a few minor tweaks to the tikz style definitions:

documentclass{article}
usepackage[svgnames]{xcolor}
usepackage{tikz}
usetikzlibrary{calc,matrix}

newcommandhochscale[1]{%hochscale{a,b,c,d,...}
  foreach r in {1,2,3,4} {
     node at ($ (M-1-3.west)+(r,-0.4) $) {r};
  }
  foreach hoch [count=r from 2, remember=r as last] in {#1} {
    node[anchor=west] at ($ (M-r-3.west)+(-0.8,0) $) {gering};
    node[anchor=east] at ($ (M-r-3.west)+(5.5,0) $)    {hoch};
    draw (M-r-3.west)+(0.5,0) -- ++(4.5,0);
    node[mypoint] (hochr) at ($ (M-r-3.west)+(hoch,0) $) {};
    ifnumr>2%
      draw[Peru](hochlast)--(hochr);
    fi
  }
  draw[dashed](M-2-3.west)+(2.5,0.5)--($ (M-last-3.west)+(2.5,-0.5) $);
}

begin{document}
  begin{tikzpicture}[mypoint/.style={circle, radius=0.5mm, draw=Peru, thick, fill=Tan}]
    matrix (M)[matrix of nodes, nodes in empty cells,
                row sep=2mm, column sep=8mm,
                row 1/.style = {anchor=center, font=largebfseries, Peru,
                                minimum height=12mm},% to allow for numbers
                column 1/.style = {text width=15mm},
                row 1 column 2/.style = {nodes={fill=white,draw=white},text width=50mm},
                column 2/.style = {nodes={rectangle,draw=Peru,fill=PapayaWhip},text width=50mm},
                column 3/.style = {text width=60mm},
                ]{
     Kategorie & Einflussgroesse &  Auspraegung in Fallstudie
               & text 1 & 
               & text 2 & 
               & text 3 & 
               & text 4 & 
               & text 5 & 
     };
     draw[Sienna, thick](M-1-1.south west) -- (M-1-3.south east);
     node[rotate=90, anchor=south] at (M-4-1) {Markt-bezogene};
     hochscale{1,2,3,2,4}
  end{tikzpicture}

end{document}

Correct answer by user30471 on March 10, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP