18

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}
2
  • 1
    Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. Commented Jul 12, 2013 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
    Commented Jul 12, 2013 at 5:25

1 Answer 1

12

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

2
  • 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
    Commented May 21, 2016 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
    Commented Jan 31, 2018 at 8:47

You must log in to answer this question.

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