0

I have the following problem. I want to label the edge of A and B with a letter f. The thing is, if I use

\draw [->] (v1) -- node[midway]{$f$} (v2);

I get this:

enter image description here

And if I use

\draw [->] (v1) -- node[midway,above]{$f$} (v2);

I get this:

enter image description here

I want "f" to be at the place of the star * - in some positive distance perpendicular to the centre of A,B. But "midway" gives me f in the center of the line and "midway, above" gives me f that is little bit to the right. Similarly, "midway, left" gives me f that is below the star.

Is there a simple way to have f in the place of * ?

EDIT: My code:

\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{tikz-cd}
\begin{document}
\begin{tikzpicture}
\node (v2) at (-1,2) {$B$};
\node (v1) at (-2.5,0.5) {$A$};
\draw [->] (v1) -- node[midway,above]{$f$} (v2);
\node at (-2,1.4) {*};
\end{tikzpicture}
\end{document}

EDIT2: Removed red crosses.

4
  • 1
    Welcome to TeXSX! Could you please pose a small compilable code reproducing the problem? This code does not seem to use tikz-cd.
    – Bernard
    May 3, 2020 at 12:46
  • Sure, just added it. And I just realized I've been actually using tikzpicture (sorry)!
    – Max Well
    May 3, 2020 at 12:55
  • This is not compilable as is. Everything with a red cross should not be in the figure?
    – Bernard
    May 3, 2020 at 12:57
  • Yes sorry to make this more confusing those red crosses shouldn't be there, I was using TizkEdt to display the diagram.
    – Max Well
    May 3, 2020 at 13:01

3 Answers 3

0

Welcome! There are two solutions. The first is that you can simply use the fill=white option to make sure the surrounding of the text painted white and then the text is drawn in black. The only caveat is that if you have a multi-coloured background (i.e., something with gradient), it will not look as nice.

Output

\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{tikz-cd}
\begin{document}
    \begin{tikzpicture}
        \node (v2) at (-1,2) {$B$};
        \node (v1) at (-2.5,0.5) {$A$};
        \draw [->] (v1) -- node[midway,fill=white]{$f$} (v2);
        \node at (-2,1.4) {*};
    \end{tikzpicture}
\end{document}

Another solution is to use xshift=-1em, yshift=1em options.

Output 2

\documentclass[10pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{tikz-cd}
\begin{document}
    \begin{tikzpicture}
        \node (v2) at (-1,2) {$B$};
        \node (v1) at (-2.5,0.5) {$A$};
        \draw [->] (v1) -- node[midway, xshift=-1em, yshift=1em]{$f$} (v2);
        %\node at (-2,1.4) {*};
    \end{tikzpicture}
\end{document}
1
  • Thank you this is the best for me!
    – Max Well
    May 7, 2020 at 12:21
2

Since you tagged your question with tikz-cd, why don't you use it?

\documentclass{article}

\usepackage{tikz-cd}

\begin{document}
\[
\begin{tikzcd}
&B\\
A\ar[ur, "f"]\\
\end{tikzcd}
\]
\end{document}

enter image description here

2
  • I need to draw commutative diagrams a lot so writing everything manually would be painful. I have been using TikzEdt so far, but it seems to me you can't do tikz-cd in it?
    – Max Well
    May 3, 2020 at 13:04
  • @MaxWell TikzEdt is no more maintained. If you would like something automatic, use something like tikzcd.yichuanshen.de or Overleaf: overleaf.com or other editors.
    – CarLaTeX
    May 3, 2020 at 13:29
0

My interpretation of the question is that you want the label be shifted along an orthogonal line of the connection. This is what the built in edge label provides you with. Otherwise, this can be achieved either by determining the slope by eyes or writing a style that measures the slope. Since the slope in this case is 45 degrees, the anchor must be at 45-90=-45. With above it will be at -90 (modulo upside down flips). With an edge label this is automatic. Nonetheless it might be worthwhile to recalculate this with calc to convince ourselves that it is so.

\documentclass[tikz,border=3mm]{standalone}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}[Midlabel/.style={to path={
  let \p1=($(\tikztotarget)-(\tikztostart)$),
    \n1={atan2(\y1,\x1)}
  in (\tikztostart) -- (\tikztotarget) node[midway,circle,anchor=\n1-90]{#1}
  }}]
\begin{scope}[local bounding box=L]
 \node (v2) at (-1,2) {$B$};
 \node (v1) at (-2.5,0.5) {$A$};
 \draw [->] (v1) -- node[midway,above]{$f$} (v2);
 \node at (-2,1.4) {*};
\end{scope}  
\begin{scope}[local bounding box=M,xshift=4cm]
 \node (v2) at (-1,2) {$B$};
 \node (v1) at (-2.5,0.5) {$A$};
 \draw [->] (v1) -- node[midway,anchor=-45]{$f$} (v2);
 \node at (-2,1.4) {*};
\end{scope}  
\begin{scope}[local bounding box=R,xshift=8cm]
 \node (v2) at (-1,2) {$B$};
 \node (v1) at (-2.5,0.5) {$A$};
 \draw[->,Midlabel={$f$}] (v1) to (v2);
 \node at (-2,1.4) {*};
\end{scope} 
\begin{scope}[local bounding box=E,xshift=12cm]
 \node (v2) at (-1,2) {$B$};
 \node (v1) at (-2.5,0.5) {$A$};
 \draw[->] (v1) to[edge label={$f$}] (v2);
 \node at (-2,1.4) {*};
\end{scope} 
\path[font=\sffamily] (L.north) node[above]{original}
 (M.north) node[above]{node anchor adjusted by hand}
 (R.north) node[above]{with \texttt{Midlabel} style}
 (E.north) node[above]{with \texttt{edge label}};
\end{tikzpicture}
\end{document}

enter image description here

As tikz-cd uses edge labels, there this is automatic. The difference comes from the fact that in the Midlabel style the node shape is taken to be a circle, which as evenly spread angle anchors.

\documentclass{article}
\usepackage{tikz-cd}
\usetikzlibrary{calc}
\begin{document}
\tikzset{Midlabel/.style={to path={
  let \p1=($(\tikztotarget)-(\tikztostart)$),
    \n1={atan2(\y1,\x1)}
  in (\tikztostart) -- (\tikztotarget) node[midway,circle,anchor=\n1-90]{#1}
  }}}
\begin{tikzcd}
 & A \\
 B\arrow[ur,"f"] &
\end{tikzcd}

\begin{tikzcd}
 & A \\
 B\arrow[ur,"f"]\arrow[ur,Midlabel={$\scriptstyle f$}] &
\end{tikzcd}
\end{document}

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .