0

I just started working with LaTeX and found a small issue. I was not able to use Greek symbols (like Omega) in the lstlisting part. I tried different methods like \Omega, $\Omega$. But it's still not working. Please see the code below for reference. Can anyone please help?

   \begin{lstlisting}
        title('X[ $\Omega$ ] vs \Omega');
        ylabel('X[\Omega]');
        xlabel('\Omega');
    \end{lstlisting}

Thank you.

2
  • 1
    Try [mathescape] as an optional argument to lstlistings.
    – Niranjan
    Feb 7, 2022 at 15:33
  • Try to set escape delimiters, e.g. (* and *) via lstset{escapeinside={(*}{*)}} and then inside lstlisting environment insert a math expression, such as $\Omega$ between defined delimiters, for instance \begin{lstlisting} (*$\Omega$*) \end{lstlisting}
    – Celdor
    Feb 7, 2022 at 20:13

1 Answer 1

0

listings allows setting delimiters that let you run LaTeX code inside. This is done via escapeinside={<opening delimniter>}{<closing delimiter>} in the command \lstset{}. For instance, I have defined both opening and closing delimiters as (* and *), respectively.

The MWE:

\documentclass{article}
\usepackage{listings}

\lstset{
  escapeinside={(*}{*)},
}


\begin{document}

\begin{lstlisting}
    title('X[ (*$\Omega$*) ] vs \Omega');
    ylabel('X[\Omega]');
    xlabel('\Omega');
\end{lstlisting}

\end{document}

enter image description here


EDIT

Another and simpler solution is to escape math expressions and add \lstset{mathescape} instead of the one with delimiters. Then, the code would work just as you type. Example:

\documentclass{article}
\usepackage{listings}

\lstset{mathescape}

\begin{document}

\begin{lstlisting}
    title('X[ $\Omega$ ] vs \Omega');
    ylabel('X[\Omega]');
    xlabel('\Omega');
\end{lstlisting}

\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .