# how to draw squares, circles and triangles [duplicate]

I'd like to explain the markers of a figure in the caption by drawing small marker symbols. I have seen in a previous question that you can do this with the tikz package.

I am using

\usepackage{tikz}

\newcommand{\square}[1]{\tikz{\filldraw[draw=#1,fill=#1] (0,0) rectangle (0.2cm,0.2cm);}}


and then \square{red} in the text when I want to draw a red square.

Could anyone show me how can I do the same for circles and triangles?

Thank you

-

## marked as duplicate by LaRiFaRi, Pouya, Thorsten, Masroor, T. VerronJul 8 '14 at 11:47

You can also use the picture environment. Or enhaced by pict2e package. – Manuel Jul 8 '14 at 11:12

If you just want something in same style, without using the useful node shapes of tikz then you can do the following. I have updated the code so it works in captions too, the replacement is to use \newrobustcmd from the etoolbox package instead of \newcommand:

\documentclass{article}

\usepackage{etoolbox}
\usepackage{tikz}

\newrobustcmd*{\square}[1]{\tikz{\filldraw[draw=#1,fill=#1] (0,0)
rectangle (0.2cm,0.2cm);}}

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

\begin{document}

\listoffigures

\section{Sample}
A square \square{red}, a circle \mycircle{green} and a triangle \mytriangle{blue}.

\begin{figure}[htp]
\centering
A figure
\caption{A square \square{red}, a circle \mycircle{green} and a triangle \mytriangle{blue}.}
\end{figure}

\end{document}


An alternative using node shape is as follows. Whilst there are more options to set, this is countered by more predefined shapes to choose between. See the pgf manual:

\documentclass{article}

\usepackage{tikz}
\usetikzlibrary{shapes}

\newcommand{\square}[1]{\tikz{\node[draw=#1,fill=#1,rectangle,minimum
width=0.2cm,minimum height=0.2cm,inner sep=0pt] at (0,0) {};}}

\newcommand{\mycircle}[1]{\tikz{\node[draw=#1,fill=#1,circle,minimum
width=0.2cm,minimum height=0.2cm,inner sep=0pt] at (0,0) {};}}

\newcommand{\mytriangle}[1]{\tikz{\node[draw=#1,fill=#1,isosceles
triangle,isosceles triangle stretches,shape border rotate=90,minimum
width=0.2cm,minimum height=0.2cm,inner sep=0pt] at (0,0) {};}}

\begin{document}

A square \square{red}, a circle \mycircle{green} and a triangle \mytriangle{blue}.

\end{document}

-
This works great in the main text, but it does not compile in the figure captions. Do you have a suggestion what the problem may be? Message: the use of \tikz@picture doesn't match it's definition. – Adam Gosztolai Jul 8 '14 at 11:51
@AdamGosztolai That is a robustness issue. I have updated my solution to take that in to account. – Andrew Swann Jul 8 '14 at 12:03
Thank you Andrew. The robustness issue is sorted now. I found that an alternative is to put an extra argument \caption[]{}. However the, \mycircle command still does not work. It says that it cannot parse this radius. It doesn't work for bigger radii either. – Adam Gosztolai Jul 8 '14 at 12:15
The extra argument, means that nothing will be written to the list of figures, which may be OK for you. The code above compiles fine for me - maybe you a have clashing definition of \mycircle lying around, the error messages can sometimes be rather cryptic; or may be you have slightly mistyped the code. – Andrew Swann Jul 8 '14 at 12:44

There are predefined mark symbols in the plotmarks library. Here are some examples

Code:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{plotmarks}
\newcommand\marksymbol[2]{\tikz[#2,scale=1.2]\pgfuseplotmark{#1};}

\begin{document}
\foreach \symb/\col in {square/red,triangle/blue,diamond/green,oplus/blue!50!red}{
\symb: \marksymbol{\symb}{\col} and \marksymbol{\symb*}{\col}
\par}
%
circle: \marksymbol{o}{yellow} and \marksymbol{*}{yellow}
\end{document}


Edit

Inside captions you have to use \protect in front of this command.

Code:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{plotmarks}
\newcommand\marksymbol[2]{\tikz[#2,scale=1.2]\pgfuseplotmark{#1};}

\begin{document}
\listoffigures

\section{Symbols}
\begin{figure}[ht]
\centering \fbox{figure}
\caption{\protect\marksymbol{heart}{blue} caption}
\end{figure}
\end{document}


If there is no \listoffigures you can alternativly use an empty optinal argument of \caption:

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{plotmarks}
\newcommand\marksymbol[2]{\tikz[#2,scale=1.2]\pgfuseplotmark{#1};}

\begin{document}
\section{Symbols}
\begin{figure}[ht]
\centering \fbox{figure}
\caption[]{\marksymbol{heart}{blue} caption}
\end{figure}
\end{document}


Or you can use \newrobustcmd from the etoolbox package like @Andrew Swann in his answer suggested.

-
This works great in the main text, but it does not work in the captions. It does not compile. – Adam Gosztolai Jul 8 '14 at 11:44

You can draw a circle in tikz using

\draw[<draw options>] (0,0) circle (1cm);


\newcommand{\circle}[1]{\tikz{\filldraw[draw=#1,fill=#1] (0,0) circle (0.2cm);}}


should do the job.

To draw a triangle, you'll have to define and connect 3 points like the following code does

\draw[<draw options>] (0,0) -- (0,2) -- (2,0)-- (0,0);


\newcommand{\triangle}[1]{\tikz{\filldraw[draw=#1,fill=#1] (0,0) -- (0,.2cm) -- (0.2cm,0) -- (0,0);}}

-

As these are to appear in text, the only change I would make to Andrew Swann's approach would be to make the size of circle, triangle and square dependent on the surrounding type. That can be done by using the em as the unit of measure:

\documentclass[12pt]{article}

\usepackage{tikz}

\newcommand{\square}[1]{\tikz{\filldraw[draw=#1,fill=#1] (0,0)
rectangle (0.6em,0.6em);}}