宏来改变 tikz 中的线条颜色

宏来改变 tikz 中的线条颜色

我对 tikz 和 latex 中的宏都很陌生,非常感谢您的帮助。我有一段用于两个类似图表的简单代码,如下所示:

\documentclass{article}
\usepackage{tikz}

\newcommand\tpdrrr{
\tikz{
\draw [dashed] (-1.5,.5) arc (90:-90:.5); % q1 right inner arc
\draw [red,thick] (-1.5,.5) arc (90:270:.5); % q2 left inner arc
\draw  [red,thick]  (-1.5,-.5) -- (-1.5,-1); % q3 vertical top
\draw  [red,thick] (-1.5,.5) -- (-1.5,0); % q4 inner vertical

}
}

\newcommand\tprdrr{
\tikz{
\draw [red,thick] (-1.5,.5) arc (90:-90:.5); % q1 right inner arc
\draw [dashed] (-1.5,.5) arc (90:270:.5); % q2 left inner arc
\draw  [red,thick]  (-1.5,-.5) -- (-1.5,-1); % q3 vertical top
\draw  [red,thick] (-1.5,.5) -- (-1.5,0); % q4 inner vertical

}
}

\begin{document}

\[\tprdrr + \tpdrrr \]

\end{document}

有人能帮我把这两个合并成一个宏吗?我可以将线条样式(虚线与红色、粗线)作为参数输入,即接受 eg\tp{r}{d}{r}{r}并输出我当前结果的代码\tprdrr?我想使用更多配置,例如\tp{r}{r}{r}{r}\t{d}{d}{d}{d}等等。所以宏会非常有用

答案1

使用ifthen包可以让你编写更短的单字母参数。这里,r对于red, thick和对于 的任何其他字母dashed,尽管d也足够了。

\documentclass{article}
\usepackage{tikz}

\usepackage{ifthen}
\newcommand{\tp}[4]{
\tikz{%
\ifthenelse{\equal{#1}{r}}
    {\draw [red,thick] (-1.5,.5) arc (90:-90:.5); }
    {\draw [dashed] (-1.5,.5) arc (90:-90:.5); }

\ifthenelse{\equal{#2}{r}}
    {\draw [red,thick] (-1.5,.5) arc (90:270:.5);  }
    {\draw [dashed] (-1.5,.5) arc (90:270:.5);  }
    
\ifthenelse{\equal{#3}{r}}
    {\draw [red,thick] (-1.5,-.5) -- (-1.5,-1);   }
    {\draw [dashed] (-1.5,-.5) -- (-1.5,-1);   }
    
  \ifthenelse{\equal{#4}{r}}
    {\draw [red,thick] (-1.5,.5) -- (-1.5,0);   }
    {\draw [dashed] (-1.5,.5) -- (-1.5,0);   }  
     
}
}
\begin{document}


\tp{r}{d}{r}{r} + \tp{d}{r}{r}{r} + \tp{r}{r}{r}{r} + \tp{d}{d}{d}{d}

\end{document}

在此处输入图片描述

答案2

作为一个单一宏的应用程序来满足您的需求。

在此处输入图片描述

\documentclass{article}
\usepackage{tikz}

\newcommand{\tp}[4]{
\tikz{%
    \draw [#1] (-1.5,.5) arc (90:-90:.5); 
    \draw [#2] (-1.5,.5) arc (90:270:.5); 
    \draw [#3] (-1.5,-.5) -- (-1.5,-1); 
    \draw [#4] (-1.5,.5) -- (-1.5,0); 
}
}

\begin{document}

\[
    \tp{red,thick}{dashed}{red,thick}{red,thick} % <= tprdrr
\]

\[
    \tp{dashed}{red,thick}{red,thick}{red,thick} % <= \tpdrrr
\]

\[
    \tp{red,thick}{red,thick}{red,thick}{red,thick} % <=  all parts are red and thick
\]

\[
    \tp{dashed}{dashed}{dashed}{dashed} % <=  all parts are dashed
\]

\end{document}

当你看到那么多重复时......这意味着它可以改进:)

相关内容