5

I am new both to tikz and macros in latex and appreciate your help. I have a simple code for two similar diagrams as follows:

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

Can someone help me combine these two as one macro where I can feed the line style (dashed vs. red,thick) as an argument, i.e. a code that takes in e.g. \tp{r}{d}{r}{r} and outputs the result of my current \tprdrr? I want to use more configurations, e.g. \tp{r}{r}{r}{r}, \t{d}{d}{d}{d}, etc. so a macro would be very useful

2 Answers 2

4

Use of the ifthen package allows you to write shorter single-letter parameters. Here, r for red, thick and any other letter for dashed, although d is also sufficient.

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

enter image description here

1
  • Thanks a lot! This is super helpful.
    – lhz157
    Apr 17 at 22:07
4

As an application to your needs in a single macro.

enter image description here

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

When you see that many repetitions though... it means it can be improved :)

1
  • 1
    Thanks a lot! I get the idea now.
    – lhz157
    Apr 17 at 16:38

You must log in to answer this question.

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