2

I was able to make a tikz matrix have an empty row by doing

\documentclass{standalone}

\usepackage{tikz}
\usetikzlibrary{matrix}
\begin{document}

\begin{tikzpicture}
  \matrix [matrix of math nodes, nodes=draw]
  {
    a & b & c \\
      &   & |[white]| d \\
    e & f & g \\
  };
\end{tikzpicture}

\end{document}

which gives

MWE

But is there a better way of doing this?

ETA

I am aware of the option nodes in empty cells. But as it says, it puts nodes in the empty cells. I don't want nodes, or at least don't what to display nodes, in that row. The following

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}

\begin{tikzpicture}
\matrix [matrix of math nodes, nodes={draw,circle}, nodes in empty cells]
  {
    a & b & c \\
      &   &   \\
    e & f & g \\
  };
\end{tikzpicture}

\end{document}

gives very different results to

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}

\begin{tikzpicture}
\matrix [matrix of math nodes, nodes=draw]
  {
    a & b & c \\
      &   &  |[white]| d \\
    e & f & g \\
  };
\end{tikzpicture}

\end{document}
2

Do you know you can simply increase the row separation of one row with [...] after \\ or of all the rows with row sep=... option?

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}
% one row sep increased:
\begin{tikzpicture}
\matrix [matrix of math nodes, nodes={draw,circle}, nodes in empty cells]
  {
    a & b & c \\[2.6ex]
    e & f & g \\
  };
\end{tikzpicture}

% all row seps increased 
\begin{tikzpicture}
\matrix [matrix of math nodes, nodes={draw,circle}, nodes in empty cells,
row sep=2.6ex]
  {
    a & b & c \\
    e & f & g \\
  };
\end{tikzpicture}

\end{document}

Both the tikzpictures of my MWE (of course, you have to use only one of them, depending on your needs) give this result:

enter image description here

0
4

The answer is yes. From the PGFPlots manual (page 647), we can draw nodes in the empty cells by putting option nodes in empty cells, so an empty row (or column) will be displayed.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}

\begin{tikzpicture}
\matrix [matrix of math nodes, nodes in empty cells]
  {
    a & b & c \\
      &   &   \\
    e & f & g \\
  };
\end{tikzpicture}

\end{document} 
3
  • Thanks. My MWE was too minimal. What if I want to draw the nodes in the first and third rows, but not the second row? – pheon Jan 26 '18 at 1:07
  • You can do it. The rows with no entries will result in blank spaces. Try with this code for example \begin{tikzpicture} \matrix [matrix of math nodes, nodes in empty cells, left delimiter=(,right delimiter=)] { & & \\ a & b & c \\ & & \\ }; \end{tikzpicture} – Cragfelt Jan 26 '18 at 1:23
  • 1
    Very good +1 also to you. – Sebastiano Jan 26 '18 at 7:26
2

Following code shows why an empty second row with & & \\ or & & [white] d\\ produces different results:

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}

\begin{tikzpicture}
\matrix [matrix of math nodes, 
    nodes in empty cells,
    nodes = draw,
    ]
  {
    a & b & c \\
      &   & d  \\
    e & f & g \\
  };
\end{tikzpicture}

\end{document}

enter image description here

As you can see in second row, empty nodes are smaller than the node which contains d. Therefore, when you declare an empty row, all nodes have default size which is related to inner sep. As soon as you insert a non empty node, this node forces increases row height.

In case that you want to draw nodes in rows 1 and 3 but not in row 2, you can define different styles for each row: row 1/.style={...}. Following code defines draw for all nodes but a particular row 2/.style doesn't draw nodes in second row and also fixes their height.

\documentclass{standalone}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}

\begin{tikzpicture}
\matrix (A) [matrix of math nodes, 
    nodes in empty cells,
    nodes = draw,
    row 2/.style = {nodes={draw=none, minimum height=1cm}}
    ]
  {
    a & b & c \\
      &   &   \\
    e & f & g \\
  };

  \draw[red,<->] (A-2-2.south)--(A-2-2.north) node[midway, right] {1 cm};
\end{tikzpicture}

\end{document}

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.