2

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}
4
  • 1
    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. Apr 21, 2016 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, 2016 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. Apr 22, 2016 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, 2016 at 10:55

1 Answer 1

3

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?.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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