2

I have made a style to make node maps using tikz. I want the formatting of arrows in a certain way, with formatted text as label. My formatting and MWE is appended below.

What I want is to define the arrow style as a function which will take three inputs: #1 (from-node) #2 (to-node) #3 (label-text). Furthermore, if I want a curved arrow as in Label 2, it should be able to take the values for out in and looseness Or I should be able to tweak the arrow code so that it can be made curved in case of just three inputs. enter image description here

\documentclass{article}
\usepackage[svgnames]{xcolor}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows}
\usetikzlibrary{positioning}

\begin{document}

\tikzstyle{block} = [rectangle, fill=gray, text centered,
minimum height=8mm,node distance=10em,  rounded corners=1mm]

\begin{tikzpicture}

\node at (0,0) [block ] (n01) {Node 01};
\node at (3,-3) [block ] (n02) {Node 02};
\node at (-3,-3) [block ] (n03) {Node 03};


\path(n01) edge[ultra thick, -latex, draw=CornflowerBlue]
node[anchor=center,fill=white] {\textcolor{Tomato}{\textit{Label 1}}}(n02);

\path(n01) edge[ultra thick, -latex,draw=CornflowerBlue,out=270,in=90,looseness=.8]
node[anchor=center,fill=white]
 {\textcolor{Tomato}{\textit{Label 2}}} (n03);

\end{tikzpicture}

\end{document}
  • 1
    manual -> Part VII Utilities -> 82 Key Management -> 82.4 Key Handlers -> .code args or .style args. – Symbol 1 Jun 2 '17 at 14:59
3

I don't think that you should make a style with arguments for that, mainly because the styles you'll create won't reduce the number of arguments you'll still need to give. I'll just propose a setup more TikZ like and try to bring some light into the matter.

First of all, \tikzstyle is deprecated, one should now set a style with the Key Handler /.style={keys} which naturally accepts one argument. So first we create the set of styles that don't take any arguments, our foundation:

block/.style={rectangle, fill=gray, text centered, minimum height=8mm,node distance=10em, rounded corners=1mm},
my line style/.style={ultra thick, -{Latex}, draw=CornflowerBlue},
my label style/.style={text=Tomato, font=\itshape, fill=white},

Now we can build up to variants which use our foundation styles but take some arguments, for example:

mylabel/.style={edge node={node[my label style]{#1}}}
my edge/.style={my line style, mylabel=#1},
my curved edge/.style n args={4}{my line style, out=#1, in=#2, looseness=#3, mylabel=#4},
normal edge/.style={my line style, out=-90, in=90, mylabel=#1},

The first one makes the label (there are several solutions for labeling an edge, I used the first one that poped into my head), it applies the label style we created beforehand and sets the text. The second applies the line style to the edge and also receives an argument which is passed to the label party. There are variants to the curved edges, one that takes four arguments for a complete flexibility and one that that has in and out preset so only the label needs to be passed, the looseness can be adjusted as normally is.

Another thing to be taken into account is the every anything styles, like every node, every edge, every path and so on. They're particulary useful when you want to set every thing to the same style and avoid repetion.

I've also taken the liberty to suggest a few other ways that the drawing can be made, using the positioning library (it could be made as a tree too, probably wiser) and the arrows.meta library which replaces the deprecated arrows library.

MWE

\documentclass{article}
\usepackage[svgnames]{xcolor}
\usepackage{tikz}
\usetikzlibrary{arrows.meta,positioning}
\begin{document}
    \begin{tikzpicture}[%
        block/.style={rectangle, fill=gray, text centered, minimum height=8mm, rounded corners=1mm}
        ,my line style/.style={ultra thick, -{Latex}, draw=CornflowerBlue}
        ,my label style/.style={text=Tomato, font=\itshape, fill=white}
        ,every edge/.style={my line style}
        ,my edge/.style={my line style, mylabel=#1}
        ,my curved edge/.style n args={4}{my line style, out=#1, in=#2, looseness=#3, mylabel=#4}
        ,normal edge/.style={my line style, out=-90, in=90, mylabel=#1}
        ,mylabel/.style={edge node={node[my label style]{#1}}}]
    \scoped[every node/.style=block, node distance=6em and 3em]{
        \path (0,0) node (n01) {Node 01}
                    node[below right= of n01] (n02) {Node 02}
                    node[below left= of n01] (n03) {Node 03};
      };    

    \path (n01) edge[mylabel=Label 1] (n02)
     edge[normal edge=Label 2] (n03);   
    \end{tikzpicture}   
\end{document}
  • Should it be -{latex} instead of -{Latex}? – Damitr Jun 6 '17 at 10:18
  • @Damitr, since the library arrows.meta has been released, the arrows names have changed, there's a whole section describing the new arrow tips and how to customize them :D. Of course this is valid only when the library is loaded, if not then you're approach is correct. – Guilherme Zanotelli Jun 6 '17 at 16:46

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.