19

I ran into an issue today, while rendering a simple diagram with Tikz. The horizontal line in the following picture is ever so slightly lower on the right hand side than on its left. Commutative Diagram

Adding a subscript to the \mathbb B on the left didn't change this behavior - neither did omitting the label h_j on the top.

Here is a minimal working code for this diagram:

\documentclass{article}
\usepackage{amssymb}
\usepackage{tikz}
\usetikzlibrary{matrix}

\begin{document}
\begin{center}
\begin{tikzpicture}
\matrix (m) 
[matrix of math nodes,row sep=4em,column sep=4em,minimum width=2em] 
{
\mathbb B & \mathbb B_{j} \\
          & \mathbb B_{i} \\ 
}; 
\path[-stealth] 
(m-1-1) edge node [above] {$h_{j}$} (m-1-2)
        edge node [below] {$h_{i}$} (m-2-2)
(m-1-2) edge node [right] {$h_{\mathbb B_{i}, \mathbb B_{j}}$} (m-2-2);
\end{tikzpicture}
\end{center}
\end{document}

Is this a known issue and does anyone know how to fix it?

18

The \mathbb{B}_j on the right is deeper than the \mathbb{B} on the left, that is why the line is lower on the right. You can just add a \vphantom{_j} to the node on the left to compensate for this.

\documentclass[border=2pt,tikz]{standalone}
\usetikzlibrary{matrix}
\usepackage{amssymb}
\begin{document} 

\begin{tikzpicture}
\matrix (m) 
[matrix of math nodes,row sep=4em,column sep=4em,minimum width=2em] 
{
\mathbb{B}\vphantom{_j} & \mathbb{B}_j \\
          & \mathbb B_{i} \\ 
}; 
\path[-stealth] 
(m-1-1) edge node [above] {$h_{j}$} (m-1-2)
        edge node [below] {$h_{i}$} (m-2-2)
(m-1-2) edge node [right] {$h_{\mathbb B_{i}, \mathbb B_{j}}$} (m-2-2);
\end{tikzpicture}

\end{document}

enter image description here

Also with node positioning instead of \matrix can look nicer:

\documentclass[border=2pt,tikz]{standalone}
\usetikzlibrary{positioning}
\usepackage{amssymb}
\begin{document}

\begin{tikzpicture}[node distance=1.5cm]
\node (b) {$\mathbb{B}$};
\node[right=of b]  (bj) {$\mathbb{B}_j$};
\node[below=of bj] (bi) {$\mathbb{B}_i$};   

\path[-stealth] 
    (b)  edge node [above] {$h_{j}$} (bj)
         edge node [below] {$h_{i}$} (bi)
    (bj) edge node [right] {$h_{\mathbb B_{i}, \mathbb B_{j}}$} (bi);
\end{tikzpicture}

\end{document}

enter image description here

  • Thank you. That fixed it. It's apparently the 'j' that messes things up. I suspected something like this and in my troubleshooting, I added an '_{k}' to the B on the left - in this case, however, the issue remains. Good to know and thanks again. – Stefan Mesken Mar 14 '16 at 11:10
  • 2
    @Stefan Different letters have different depth/height, so using _{k} need not be equivalent to using _{j}. – Bakuriu Mar 14 '16 at 12:28
11

I suggest using tikz-cd:

\documentclass{article}
\usepackage{amsmath,amssymb,tikz-cd}

\begin{document}

\[
\begin{tikzcd}
\mathbb{B} \arrow[r,"h_j"] \arrow[dr,swap,"h_i"] &
  \mathbb{B}_j \arrow[d,"h_{\mathbb{B}_i,\mathbb{B}_j}"]
\\
& \mathbb{B}_i
\end{tikzcd}
\]

\end{document}

enter image description here

  • 1
    Why do you need to use tikz-cd to compensate the different text depth? Also, adding a little information on tikz-cd would be nice... – Dux Mar 14 '16 at 11:06
  • 1
    @Dux There is plenty of information on the site about tikz-cd; I know well this doesn't directly answers the question, but the syntax is way simpler than using \matrix and the depth problem is completely irrelevant. Adding text depth=0.5pt to every node is definitely not the solution, because some node could have much larger depth, think to a fraction. I won't downvote your answer, though. – egreg Mar 14 '16 at 11:42
  • Thank you very much :) This is definitely a point that one could debate over, I like to create styles for the nodes in my picture that cover all their content. But this is just me ofc. I do not know much about tikt-cd, that's why I suggested a little input in your answer instead of just giving the name. Saves one visit to google ;) – Dux Mar 14 '16 at 11:51
  • In my next document, I will probably give tikz-cd a shot. Thank you for your answer. – Stefan Mesken Mar 14 '16 at 12:04
7

As was already mentioned, the problem is the text depth of the content of your nodes. One simple way of adjusting this is to reserve equal space for this in every node by specifying the text depth manually using the key text depth:

\documentclass[tikz]{standalone}
\usepackage{amssymb}
\usetikzlibrary{matrix}
\begin{document}
\begin{tikzpicture}[
    every node/.style={text depth=0.5pt}
]

\matrix (m) 
[matrix of math nodes,row sep=4em,column sep=4em, minimum width=2em] 
{
\mathbb B & \mathbb B_{j} \\
          & \mathbb B_{i} \\ 
}; 
\path[-stealth] 
(m-1-1) edge node [above] {$h_{j}$} (m-1-2)
        edge node [below] {$h_{i}$} (m-2-2)
(m-1-2) edge node [right] {$h_{\mathbb B_{i}, \mathbb B_{j}}$} (m-2-2);
\end{tikzpicture}
\end{document}
2

XYpic provides another alternative. It gets the alignment correct in this case without special adjustment. The syntax is very similar to tikz-cd in simple cases. (Although I think it would be more correct to say: the syntax of tikz-cd is very similar to xymatrix... )

\documentclass[margin=5mm]{standalone}
\usepackage{amssymb}
\usepackage[matrix,pdf,arrow]{xy}
\begin{document}
\begin{xymatrix}{
  \mathbb{B} \ar[rd]_{h_i} \ar[r]^{h_j}
  &\mathbb{B}_j \ar[d]^{h_{\mathbb{B}_i,\mathbb{B}_j}}
\\
  &\mathbb{B}_i
\\}
\end{xymatrix}
\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.