# defining a tikz shape with different fills in a figure caption

In the graph captions of my thesis, I want to define 3 triangles with 3 fills (yellow, red, and black). For one triangle, it works fine. How can I define the two other triangles with different fills.

Below is the MWE of thesis and the chapter:

\documentclass[b5paper,twoside,10pt,openright]{book}
\usepackage[english]{babel}
\usepackage{charter}
\usepackage{etoolbox}
\usepackage{tikz}
\definecolor{mycolor1}{RGB}{255,255,0}      % yellow
\definecolor{mycolor2}{RGB}{255,0,0}        % red
\definecolor{mycolor3}{RGB}{0,0,0}          % black

\newrobustcmd*{\mytriangle}[1]{\tikz{\draw[thick, fill=mycolor1] ((0,0) -- (0.2cm,0) -- (0.1cm,0.2cm) -- cycle;}}

%\newrobustcmd*{\mytriangle}[1]{\tikz{\draw[thick, fill=mycolor2] ((0,0) -- (0.2cm,0) -- (0.1cm,0.2cm) -- cycle;}}
%\newrobustcmd*{\mytriangle}[1]{\tikz{\draw[thick, fill=mycolor2] ((0,0) -- (0.2cm,0) -- (0.1cm,0.2cm) -- cycle;}}

\begin{document}

this is my thesis

\include{introduction}

\end{document}


This is the code for the chapter Introduction:

\chapter{introduction}

\begin{figure}[htp]

A figure
\caption{This is a triangle {\mytriangle{mycolor1}}}

\end{figure}

• Define just one command accepting as (mandatory) argument the fill color: \newrobustcmd*{\mytriangle}[1]{\tikz{\draw[thick, fill=#1] (0,0) -- (0.2cm,0) -- (0.1cm,0.2cm) -- cycle;}}. Invoke it as \mytriangle{<color>}. Is this what you were trying to achieve? – Pier Paolo Dec 31 '14 at 10:10
• @PierPaolo Your code gives me one triangle with one fill; how do I define the same triangle now with 2 more different fills? If I define color with different fills in your argument, I get the error that \mytriangle is already defined. I want the caption as {this is a triangle \mytriangle{mycolor1} and another triangle \mytriangle{mycolor2}) – abhishek Dec 31 '14 at 10:29
• Could you post a picture (even drawn by hand) of what you want? – Pier Paolo Dec 31 '14 at 10:49
• @PierPaolo see the picture; apologies if I did not explain it clearly – abhishek Dec 31 '14 at 10:54
• @abhishek, Pier Paolo's comment should do exactly what you want... Just type exactly what you said before (this is a triangle \mytriangle{mycolor1} and another triangle \mytriangle{mycolor2}) with the definition as provided and it should work. – Paul Gessler Dec 31 '14 at 11:50

## 1 Answer

According to @Pier Paolo comment, you can make use of the command \mytriangle with an argument. The argument is used to pass the colour you want.

1. Note that you don't need to define those colours. They are already defined when you load tikz. So you just use \mytriangle{red}.

2. Note that your original triangle (produced with (0,0) -- (0.2cm,0) -- (0.1cm,0.2cm) -- cycle) is not an equilateral triangle.

So I decided to make use of polar coordinates to create an equilateral one inscribed on a circle with radius \radius. Note the use of

\newlength{\radius} \setlength{\radius}{.2cm}


3. I also defined other two commands which work in the same way, with mandatory argument for colours.

MWE

\documentclass{report}
\usepackage{etoolbox}
\usepackage{tikz}
\usepackage{lipsum}

%%\definecolor{mycolor1}{RGB}{255,255,0}      % yellow
%%\definecolor{mycolor2}{RGB}{255,0,0}        % red
%%\definecolor{mycolor3}{RGB}{0,0,0}          % black
\newlength{\radius}
\setlength{\radius}{.2cm}

\newrobustcmd*{\mytriangle}[1]{\tikz{\draw[thick, fill=#1] (90:\radius) -- (210:\radius) -- (330:\radius) -- cycle;}}

\newrobustcmd*{\mysquare}[1]{\tikz{\draw[thick, fill=#1] (45:\radius) -- (135:\radius) -- (225:\radius) -- (315:\radius) -- cycle;}}

\newrobustcmd*{\mycircle}[1]{\tikz{\draw[thick, fill=#1] (0,0)  circle (\radius);}}

\begin{document}
\lipsum*[1] \mytriangle{yellow} \quad \mytriangle{red} \quad \mytriangle{black}
%
\lipsum*[2] \mysquare{yellow} \quad \mysquare{red} \quad \mysquare{black}
%
\lipsum*[5] \mycircle{yellow} \quad \mycircle{red} \quad \mycircle{black}

\end{document}


• In this way, it also works in my document. But, when I put it in a "Chapter" of a thesis, it does not work. Can you try that? I am trying it too. – abhishek Dec 31 '14 at 12:10
• @abhishek, be careful. I loaded the package lipsum only to produce random text. In fact, you will need to load only tikz and etoolbox. What is the error? – Sigur Dec 31 '14 at 12:13
• Yes, I am aware. Ok, it seems to work now. I will place this in my thesis and see how it works out. I am very curious. – abhishek Dec 31 '14 at 12:15
• Yes, FINALLY, it all works fine now. I am glad! thanks all for the help! – abhishek Dec 31 '14 at 13:01
• Sorry, I am wrong. I used your command with fill=#1 and defined different colors with mycolor , like what you said. Sorry about the confusion. – abhishek Dec 31 '14 at 13:26