TikZ:\newcommand 带有可选的 TikZ 选项

TikZ:\newcommand 带有可选的 TikZ 选项

我想创建一个带有一些默认 TikZ 选项的新命令,您可以使用可选参数覆盖这些选项。如果按预期工作,下面的代码将在 A 和 B 之间生成一条红线,并在 A 和 C 之间生成一条带箭头的半透明绿线。我该如何实现这一点?

\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

正如@TomBombadil 回答的那样这个问题一种方法是

\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}

结果如下

在此处输入图片描述

正如 @percusse 所建议的,只需要对您的代码进行一点修改

\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}

结果是

在此处输入图片描述

相关内容