Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

For some reason, the column doesn't align properly with matrix B. And I can't figure out why. I hate having to resort to using yshift this many millimeter this and this many millimeter for this.

Column of B doesn't align with matrix B

\documentclass[11pt]{standalone}

\usepackage{tikz}

\usetikzlibrary{shapes}
\usetikzlibrary{arrows}
\usetikzlibrary{positioning}
\usetikzlibrary{calc}

\definecolor{shade1}{rgb}{0.9, 0.9, 0.9}
\definecolor{shade2}{rgb}{0.8, 0.8, 0.8}
\definecolor{shade3}{rgb}{0.65, 0.65, 0.65}
\definecolor{shade4}{rgb}{0.45, 0.45, 0.45}

\begin{document}

\begingroup
\newlength{\matrixSize}
\setlength{\matrixSize}{2cm}
\newlength{\matrixSep}
\setlength{\matrixSep}{4mm}
\begin{tikzpicture}[%
    auto,
    box/.style={%
      draw,
      rectangle,
    },
    matrix/.style={%
      box,
      fill=shade2,
      minimum size=2cm,
      inner sep=0pt,
      node distance=\matrixSep,
    },
    cell/.style={%
      box,
      fill=shade3,
      minimum size=0.1\matrixSize,
      inner sep=0pt,
    },
  ]

  % Matrices
  \node [matrix]             (C) {C};
  \node [matrix, left=of C]  (A) {A};
  \node [matrix, above=of C] (B) {B};

  % Cell to calculate in C
  \node [cell] at ([xshift=-0.2\matrixSize, yshift=0.2\matrixSize]
    C.south east) (cell) {};

  % Row from A
  \path let \p1 = (cell),
            \p2 = (A.base),
            \p3 = (A.west),
            \p4 = (A.east)
         in
           node [%
             cell,
             minimum width=\x4-\x3-\pgflinewidth,
           ] at ([yshift=\y1-\y2] A.base) (row) {};

  % Column from B
  \path let \p1 = (cell),
            \p2 = (B.base),
            \p3 = (B.south),
            \p4 = (B.north)
         in
           node [%
             cell,
             minimum height=\y4-\y3-\pgflinewidth,
           ] at ([xshift=\x1-\x2] B.base) (col) {};
\end{tikzpicture}
\endgroup

\end{document}
share|improve this question
1  
You need node [ cell, minimum height=\y4-\y3-\pgflinewidth, ] at ([xshift=\x1-\x2] B.center) (col) {}; –  Gonzalo Medina Sep 29 '11 at 17:35
add comment

1 Answer

up vote 6 down vote accepted

You need to use the B.center anchor instead of the B.base one; the latter is located a little lower than the former. In the following code I show the difference, using a red circle for B.center and a blue circle for B.base:

\documentclass[11pt]{standalone}

\usepackage{tikz}

\usetikzlibrary{shapes}
\usetikzlibrary{arrows}
\usetikzlibrary{positioning}
\usetikzlibrary{calc}

\definecolor{shade1}{rgb}{0.9, 0.9, 0.9}
\definecolor{shade2}{rgb}{0.8, 0.8, 0.8}
\definecolor{shade3}{rgb}{0.65, 0.65, 0.65}
\definecolor{shade4}{rgb}{0.45, 0.45, 0.45}

\begin{document}

\begingroup
\newlength{\matrixSize}
\setlength{\matrixSize}{2cm}
\newlength{\matrixSep}
\setlength{\matrixSep}{4mm}
\begin{tikzpicture}[%
    auto,
    box/.style={%
      draw,
      rectangle,
    },
    matrix/.style={%
      box,
      fill=shade2,
      minimum size=2cm,
      inner sep=0pt,
      node distance=\matrixSep,
    },
    cell/.style={%
      box,
      fill=shade3,
      minimum size=0.1\matrixSize,
      inner sep=0pt,
    },
  ]

  % Matrices
  \node [matrix]             (C) {C};
  \node [matrix, left=of C]  (A) {A};
  \node [matrix, above=of C] (B) {B};

  % Cell to calculate in C
  \node [cell] at ([xshift=-0.2\matrixSize, yshift=0.2\matrixSize]
    C.south east) (cell) {};

  % Row from A
  \path let \p1 = (cell),
            \p2 = (A.base),
            \p3 = (A.west),
            \p4 = (A.east)
         in
           node [%
             cell,
             minimum width=\x4-\x3-\pgflinewidth,
           ] at ([yshift=\y1-\y2] A.base) (row) {};

  % Column from B
  \path let \p1 = (cell),
            \p2 = (B.base),
            \p3 = (B.south),
            \p4 = (B.north)
         in
           node [%
             cell,
             minimum height=\y4-\y3-\pgflinewidth,
           ] at ([xshift=\x1-\x2] B.center) (col) {};

\node[circle,red, draw] at (B.center) {};
\node[circle,blue, draw] at (B.base) {};
\end{tikzpicture}
\endgroup

\end{document}

enter image description here

A little additional explanation: if the text label field of a node with rectangle shape is empty, the .base and .center anchors will be equal; however, adding a text label produces a vertical shifting of the .base anchor, as can be seen with the following code:

\documentclass[11pt]{standalone}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}[node distance=1cm and 1cm]
  \node[name=t,shape=rectangle] {};
  \foreach \anchor/\placement in {center/above, base/below}
    \draw[shift=(t.\anchor)] plot[mark=x] coordinates{(0,0)} 
      node[\placement] {\scriptsize\texttt{(t.\anchor)}};
  \node[name=s,shape=rectangle,xshift=3cm] {\Huge A};
  \foreach \anchor/\placement in {center/above,base/below}
    \draw[shift=(s.\anchor)] plot[mark=x] coordinates{(0,0)}
      node[\placement] {\scriptsize\texttt{(s.\anchor)}};
\end{tikzpicture}

\end{document}

enter image description here

share|improve this answer
    
What's the reason behind the difference between .base and .center? –  gablin Sep 29 '11 at 18:22
1  
@gablin: if the text label field of a node with rectangle shape is empty, the .base and .center anchors will be equal; however, adding a text label changes the base anchor for the rectangle so that it coincides with the base of the text label contents. You can see this with the following code: –  Gonzalo Medina Sep 29 '11 at 18:45
1  
@gablin: \documentclass[11pt]{standalone} \usepackage{tikz} \begin{document} \Huge \begin{tikzpicture} \node[name=t,shape=rectangle] {}; \foreach \anchor/\placement in {center/above, base/below} \draw[shift=(t.\anchor)] plot[mark=x] coordinates{(0,0)} node[\placement] {\scriptsize\texttt{(t.\anchor)}}; \node[name=s,shape=rectangle,xshift=3cm] {A}; \foreach \anchor/\placement in {center/above,base/below} \draw[shift=(s.\anchor)] plot[mark=x] coordinates{(0,0)} node[\placement] {\scriptsize\texttt{(s.\anchor)}}; \end{tikzpicture} \end{document} –  Gonzalo Medina Sep 29 '11 at 18:45
    
I see. Thanks for explaining! –  gablin Sep 29 '11 at 19:12
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.