Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

This question already has an answer here:

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

share|improve this question

marked as duplicate by LaRiFaRi, Pouya, Thorsten, Masroor, T. Verron Jul 8 at 11:47

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

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

4 Answers 4

up vote 6 down vote accepted

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:

Sample output

\documentclass{article}

\usepackage{etoolbox}
\usepackage{tikz}

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

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

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

Second sample

\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}
share|improve this answer
    
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 at 11:51
    
@AdamGosztolai That is a robustness issue. I have updated my solution to take that in to account. –  Andrew Swann Jul 8 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 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 at 12:44

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

enter image description here

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.

enter image description here

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.

share|improve this answer
    
This works great in the main text, but it does not work in the captions. It does not compile. –  Adam Gosztolai Jul 8 at 11:44

You can draw a circle in tikz using

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

For your case

\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);

For your case

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

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);}}

\newcommand{\mycircle}[1]{\tikz{\filldraw[draw=#1,fill=#1] (0,0) circle [radius=0.3em];}}

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

\begin{document}

\footnotesize A square \square{red}, \Large a circle \mycircle{green} \Huge and a triangle \mytriangle{blue}.![Circle square rectangle sized.][1]

\end{document}

A square, circle and triangle sized according to surrounding text.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.