# Write mathematics in a caption

Sometimes I want to write mathematics (like \in - is an element of). While the element-sign is shown, LaTeX shows errors when compiling saying:

Missing $inserted. Extra }, or forgotten$.

Missing } inserted.

When then surrounding any \in with $ and $ it adds a blank line before and after the line containing \in. I only want to have the simple sign, not the additional space.

This is my code without $...$ around the \in:

\documentclass[11pt, a4paper]{article}
\usepackage[portrait, margin=1in]{geometry}
\usepackage{amsmath}
\usepackage[linesnumbered,ruled]{algorithm2e}

\begin{document}
\begin{algorithm}
\SetKwInOut{Input}{Input}
\SetKwInOut{Output}{Output}

\underline{function myAlgo} $(G)$\;
\Input{A Graph $G = (V, E)$ with v in V}
\Output{nothing yet}
\tcp{does it for ever v in V}
doSth()\;
return $G$\;
\caption{Does sth for every vertex v \in V}
\end{algorithm}
\end{document}

• what you probably want is $v \in V$. the \in must be in math mode (the part inside $...$ as you have already used correctly for $G = (V, E)$). since \in is recognized as math, tex will switch into math mode if it isn't already there, and will therefore report a missing $ when it encounters something that can't be math. – barbara beeton Apr 21 '16 at 18:06 • thats it thanks, unfortunately you wrote a comment instead of an answer, so I cannot denote you with +1 – Kaspatoo Apr 22 '16 at 9:50 • i've posted an answer now. i commented instead of answering for two reasons: first, this is a really elementary question for which the information can be found in any good introduction to tex, and second, it has probably been answered here already, and is thus a duplicate, but i'm in a situation where i can't easily check. i'll add a link to a good introduction to my answer. – barbara beeton Apr 22 '16 at 11:37 • hi, yes of course I thought of that but I may am too silly to find them. I gave up after about 20 minutes of searching for this. – Kaspatoo Apr 23 '16 at 10:55 ## 1 Answer what you probably want is $v \in V$. the \in must be in math mode (the part inside $...$ as you have already used correctly for $G = (V, E)$). since \in is recognized as math, tex will switch into math mode if it isn't already there, and will therefore report a missing$ when it encounters something that can't be math.

this is an elementary principle of tex, so reading a good introduction would be a good idea. i recommend the books by nicola talbot. some other good advice is given at Why should I buy a TeX/LaTeX guide book?.