Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using $ (dollar) as escapeinside symbol #179

Closed
ysalmon opened this issue Aug 18, 2017 · 6 comments
Closed

Using $ (dollar) as escapeinside symbol #179

ysalmon opened this issue Aug 18, 2017 · 6 comments
Labels

Comments

@ysalmon
Copy link

ysalmon commented Aug 18, 2017

Most of the code I typeset contains metasyntactic elements that should be displayed in math-mode.

I have been using listings for several years and am trying to switch to minted. I thus have lots of code that uses $\whatever$ to escape to math mode.

I understand that minted has several kinds of math escaping depending on whether we are in a comment or not. However I could not set it to accept $ as a math escape character in normal code.

I tried escapeinside=$$ and escapeinside=\$\$.

The languages I use (Python, SQL and Caml) do not use this symbol as a part of their syntax.

@gpoore gpoore added the bug label Aug 18, 2017
@gpoore
Copy link
Owner

gpoore commented Aug 18, 2017

That's a bug. escapeinside=$$ seems to work under Windows, but Linux needs escapeinside=\\$\\$ so that when the Pygments highlighting command is executed, the dollar signs are treated literally.

I'm working on a redesigned for minted 3.0 that will avoid these sorts of issues. Until that is ready, some characters like $ will need operating system-dependent explicit escaping to avoid shell expansion.

@ysalmon
Copy link
Author

ysalmon commented Aug 18, 2017

Thanks for your help. Alas, it does not work either : the $ symbol is not displayed anymore in the typeset code, but the content between $ is typeset as code and not as math mode. Using control sequences causes a compilation failure :

\documentclass{article}
\usepackage[cache=false]{minted}
\setminted{mathescape, escapeinside=\\$\\$}
\newminted[lstpython]{python3}{}
\begin{document}
\begin{lstpython}
def toto(x) :
  # toto $x$
  return $\sqrt{x}$
\end{lstpython}
\end{document}
! Missing $ inserted.
<inserted text> 
                $
l.4   \PYG{k}{return} \PYG{esc}{\sqrt{x}}

@gpoore
Copy link
Owner

gpoore commented Aug 18, 2017

I missed that you were trying to escape to math mode. escapeinside just escapes to LaTeX, but doesn't set the mode according to the escape character. So you could use something like this:

\documentclass{article}
\usepackage[cache=false]{minted}
\setminted{mathescape, escapeinside=||}
\newminted[lstpython]{python3}{}
\begin{document}
\begin{lstpython}
def toto(x) :
  # toto $x$
  return |$\sqrt{x}$|
\end{lstpython}
\end{document}

@ysalmon
Copy link
Author

ysalmon commented Aug 18, 2017

Oh, sorry, I missed this subtlety in the manual. Being able to escape to non-mathmode LaTeX seems nice. Is it also possible regarding comments --- it seems not. The way Pygments manages escapes seems rather cumbersome, if I understand your post there correctly.

@gpoore
Copy link
Owner

gpoore commented Aug 18, 2017

You can use texcomments to treat everything in comments as LaTeX.

Yes, the current Pygments escaping has issues...if that is ever fixed, it may be possible to have more escape-related features.

@ysalmon
Copy link
Author

ysalmon commented Apr 28, 2021

I think I got a (dirty) way to escape to math mode by default. When a piece of text, say xxxx, is escaped, it is translated to a \PYG{esc}{xxxx}. So one can hack the style to have a \ensuremath slapped around the text. When using say the borland style, I thus define

\def\PYGborland@tok@esc{\let\PYGborland@ff=\ensuremath}

I assume the ff construct is originally tasked with changing the font face, so it is not entirely inappropriate to subvert it in this fashion.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants