16

I want to create a new command with some default TikZ options that you can override using an optional argument. If working as intended the code below would generate a red line between A and B, and a translucent green line with arrows between A and C. How can I accomplish this?

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes}

\begin{document}

\newcommand{\relation}[3][]
{
  % this needed to be modified somehow...
  \path [draw, red] (#2) -- (#3);
}

\begin{tikzpicture}
  \node [draw] (A) at (0, 0) {A};
  \node [draw] (B) at (3, 0) {B};
  \node [draw] (C) at (3, 3) {C};
  \relation{A}{B}
  \relation[<->, color=green!10]{A}{C}
\end{tikzpicture}

\end{document}
  • 1
    Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. – texenthusiast Jul 12 '13 at 4:56
  • 5
    You already have the solution. Put draw,red,#1 and it's good to go. However, it is better to use draw=red such that you can easily override it with draw=none without having the red color set for fill etc. – percusse Jul 12 '13 at 5:25
10

As @TomBombadil answered in this question a way to do that is

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes}

\newcommand{\CloudRed}[2][180]% [angle], content
{   \begin{tikzpicture}[overlay]
    \node[align=center, draw,  fill=red, text=white, cloud callout, cloud puffs=17, cloud puff arc=140, callout pointer segments=3, anchor=pointer, callout relative pointer={(#1:2 cm )}, aspect=4,scale=0.5] at (-3ex,0.5ex) {#2};
\end{tikzpicture}
}

\begin{document}

  \begin{itemize}
        \item XY Resistive MM\Cloud{Thank you Rui}
    \item Manufactured by Rui de Oliveira\Cloud[160]{Thanks again}
    \item bla bla bla\Cloud[120]{Thanks yet another time}
    \end{itemize}
\end{document}

The result looks like

enter image description here

As @percusse suggested just a little modification is needed to your code

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{shapes}

\begin{document}

\newcommand{\relation}[3][]
{
  % this needed to be modified somehow...
  \path [draw=red,#1] (#2) -- (#3);
}

\begin{tikzpicture}
  \node [draw] (A) at (0, 0) {A};
  \node [draw] (B) at (3, 0) {B};
  \node [draw] (C) at (3, 3) {C};
  \relation{A}{B}
  \relation[<->, color=green!40!black!100]{A}{C}
\end{tikzpicture}

\end{document}

The result is

enter image description here

  • 1
    Your first code has problems. Here's a suggested fix based on your code (I've made colors as parameters too): pastebin.com/9VAT0YLJ – PatrickT May 21 '16 at 17:25
  • I'm wondering how this can be extended to also use a an argument in normal braces, as in \mynode[draw] (name) {something} ? – daniatic Jan 31 '18 at 8:47

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.