5

I'm basically asking this question, because the answer there is not working for the case with -- lines.

Say you have a line like so:

\draw (0,0) -- (3,3) node[sloped, midway,above]{Hey!};

Then is it possible to make a style such that the code below produces the same result as above?

\draw[heyEnd](0,0) -- (3,3);

I suspect that it should be very possible, because arrow tips makes this effect.

Full example:

\documentclass[margin=10pt]{standalone}
\usepackage{tikz}
\begin{document}
  \begin{tikzpicture}
    \tikzset{
      heyEnd/.style={
          every edge/.style={
              edge node={node[sloped, midway,above]{Hey!}},
              draw
            }
        }
    }

    %% Intended result:
    % \draw (0,0) -- (3,3) node[sloped, midway,above]{Hey!};

    %% This works:
    % \draw[heyEnd](0,0) edge (3,3);

    % This does not, but I want it to
    \draw[heyEnd](0,0) -- (3,3);
  \end{tikzpicture}
\end{document}

(Possibly) related:

  • is edge the same as "--" ? Probably you have to do some style settings here as well (when possible). – albert Jun 17 '18 at 13:39
  • I don't think that edge is equivalent to --. If it is supposed to be equivalent, then I've discovered a bug, heh :) I don't know when one should be used in favour of the other. I do, however, know that -- is often used and I'd like to know how to do this. Mostly for educational purposes. – Andreas Storvik Strauman Jun 17 '18 at 13:42
  • 1
    @albert Here is an explanation of edge vs --. They are not supposed to be equivalent :) – Andreas Storvik Strauman Jun 17 '18 at 13:43
  • From the TikZ manual: "Like a node the edge generated by the edge operation is not part of the main path, but is added only later." Maybe this is the reason... – Phelype Oleinik Jun 17 '18 at 13:47
  • @PhelypeOleinik That might very well be the reason. I'd still want to know how to do it on -- paths using styles :) – Andreas Storvik Strauman Jun 17 '18 at 13:49
8

I don't now if that's the answer you are looking for but you can of course achieve the same for -- lines with decorations.

\documentclass[margin=10pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{decorations.markings}
\begin{document}
  \begin{tikzpicture}
    \tikzset{
      heyEnd/.style={
          every edge/.style={
              edge node={node[sloped, midway,above]{Hey!}},
              draw
            }
        },
     heyMark/.style={postaction={decorate,decoration={markings,
     mark=at position 0.5 with {\node[midway,above,sloped]{Hey!}; }}}}  
    }

    %% Intended result:
    % \draw (0,0) -- (3,3) node[sloped, midway,above]{Hey!};

    %% This works:
    \draw[heyEnd](0,0) edge (3,3);

    % This works now
    \draw[heyMark](4,0) -- (7,3);
  \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.