3

I'd like to switch from tikz to tikz-cd to draw diagrams of arrows (I just started practicing, so I don't know much), and I can't figure out how to translate the following node style:

\documentclass[a4paper,11pt]{article}

\usepackage{tikz}
\usetikzlibrary{matrix,arrows}

\begin{document}

\begin{tikzpicture}
[grade/.style={sloped,fill=white,inner sep=1pt,font=\tiny}]
\matrix (m) [matrix of math nodes, 
             row sep=2.5em, column sep=3em,
             text height=1.5ex, text depth=0.25ex]
{\cdot &       \\
       & \cdot \\};
\path[->,font=\scriptsize]
(m-1-1) edge node[auto] {$f$} (m-2-2)
        edge node[grade] {$[1]$} (m-2-2);
\end{tikzpicture}

\end{document}

enter image description here

I tried out some basic solution, but it just does't look right:

\documentclass[a4paper,11pt]{article}

\usepackage{tikz-cd}

\begin{document}

\begin{tikzcd}
\cdot \ar[dr,"f", "(1)" description, sloped, font=\tiny] & \\
& \cdot
\end{tikzcd}

\end{document}

enter image description here

The main problems are:

  • I can't figure out how to change the font size of the label;

  • the labels overlap;

  • the options affect both labels.

Plus it's very annoying not being able to type directly square brackets in the label.

Any suggestion?

3

Two diagrams to compare:

enter image description here

The first diagram is the original one using a matrix of math nodes. The second one uses tikz-cd.

The code:

\documentclass[a4paper,11pt]{article}
\usepackage{tikz-cd}
\usetikzlibrary{matrix,arrows}

\tikzset{
grade/.style={sloped,fill=white,inner sep=1pt,font=\tiny}
}

\begin{document}

\begin{tikzpicture}
\matrix (m) [matrix of math nodes, 
             row sep=2.5em, column sep=3em,
             text height=1.5ex, text depth=0.25ex]
{\cdot &       \\
       & \cdot \\};
\path[->,font=\scriptsize]
(m-1-1) edge node[auto] {$f$} (m-2-2)
        edge node[grade] {$[1]$} (m-2-2);
\end{tikzpicture}

\begin{tikzcd}[
  row sep=2.5em,
  column sep=3em,
  description/.append style={grade},
]
\cdot\ar[dr,phantom,"f" above right=1pt]\ar[dr,"{[1]}" description] & \\
& \cdot
\end{tikzcd}

\end{document}

To answer the specific questions at the end of the question:

  • You can use description/.style or description/.append style to change the style for description. In particular, I used description/.append style={grade} in the second diagram.

  • In the case of nested expressions with brackets, you need an extra pair of braces for the inner one, as in

    \ar[dr,"{[1]}" description];
    

    otherwise, the first ] is considered the closing bracket for the opning one in \ar[.

  • Instead of one arrow with two labels as in

    \ar[dr,"f", "(1)" description, sloped, font=\tiny]
    

    you can use two arrows; in this way, you have individual control over the elements:

    \ar[dr,phantom,"f" above right=1pt]\ar[dr,"{[1]}" description]
    

    since the first arrow is of style phantom, it isn't effectively drawn.

  • You can use left=<length>, or right=<length> (or similar) to move labels. I used

    \ar[dr,"{f}" above right=1pt]
    
| improve this answer | |
1

I found out that using \tikzcdset the problem can be solved in an even more compact way:

\documentclass[a4paper,11pt]{article}
\usepackage{tikz-cd}
\usetikzlibrary{matrix,arrows}

\tikzcdset{diagrams={row sep=2.5em, column sep=3em},
grade/.style={description,sloped,fill=white,inner sep=1pt,font=\tiny}}
\newcommand{\gar}[1]{\ar[#1,phantom,"{[1]}" grade]}

\begin{document}

\begin{tikzcd}
\cdot \ar[dr,"f"] \gar{dr} &       \\
                           & \cdot
\end{tikzcd}

\end{document}

enter image description here

One has just to be careful and put \gar after the arrow, otherwise the label will be placed underneath the edge.

| improve this answer | |

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.