Repository: pygments/pygments

Issue #825: `escapeinside` is fragile with some lexers
Created: 2019-08-31T17:33:38Z
(Original issue 1118 created by g_poore on 2015-06-23T22:26:13.182331+00:00)
If I understand correctly, escapeinside works by tokenizing to obtain strings and comments, then merging all non-string and non-comments tokens, then scanning these merged tokens for escapes, and finally scanning everything that wasn't escaped for normal tokens. So it isn't possible to have escapes inside of strings and comments. This triple-scan approach can cause escapeinside to fail in non-obvious ways.
For example, in the bash lexer the backslash \ and immediately following character is treated as a String.Escape token. Because of this, standard LaTeX commands of the form \command will always be treated as starting with a type of string, even when they occur in a region "protected" by escapeinside. More details about this particular case are here.
Basically, any time that the LaTeX code contained within an escape looks like a string or comment for the current lexer, escapeinside may fail. If it would be possible to tokenize everything in a single stage, with escapes having the highest priority, that might be a solution.

Duplicate issue #1485: Escape in strings
Created: 2020-06-23T19:52:04Z
Currently pygments doesn't support escape in strings, which makes typesetting with minted (which uses pygments) in latex very limited. Actually there is a way to improve the latex formatter so that raw latex code can be embedded in strings, which can greatly enhance its ability. As an example, this shows how to render in different styles and languages, much similar to plain fancyvrb, but with auto code highlighting:
\begin{minted}[escapeinside=||,escapeinstr]{java}
public class HelloWorld {
    public static void main(String[] args) {
        System.out.println("abc.|{\rmfamily{|rmfamily|}}|.xyz");
        System.out.println("abc.|{\sffamily{|sffamily|}}|.xyz");
        System.out.println("abc.|{\ttfamily{|ttfamily|}}|.xyz");
        System.out.println("abc.|{\mdseries{|mdseries|}}|.xyz");
        System.out.println("abc.|{\bfseries{|bfseries|}}|.xyz");
        System.out.println("abc.|{\upshape{|upshape|}}|.xyz");
        System.out.println("abc.|{\itshape{|itshape|}}|.xyz");
        System.out.println("abc.|{\slshape{|slshape|}}|.xyz");
        System.out.println("abc.|{\scshape{|scshape|}}|.xyz");
        System.out.println("abc.|{\large{|large|}}|.xyz");
        System.out.println("abc.|{\small{|small|}}|.xyz");
        System.out.println("abc.|{\tiny{|tiny|}}|.xyz");
        System.out.println("abc.|{\japanesefont{|日本語|}}|.xyz");
        System.out.println("abc.|\RL{\arabicfont{|اللغة العربية|}}|.xyz");
        System.out.println("abc.|{\color{cyan}{cyan}}|.xyz");
        System.out.println("abc.|{\color{magenta}{magenta}}|.xyz");
        System.out.println("abc.|{\color{yellow}{yellow}}|.xyz");
        System.out.println("abc.|{\color{black}{black}}|.xyz");
    }
}
\end{minted}
I understand this is a python program not a latex one, but the popular latex package minted uses this program as an underlying tool. When pygments is patched, it is very easy to patch minted:
\minted@def@optcl@switch{escapeinstr}{-P escapeinstr}

Closing comment (2020-06-23T20:35:21Z): Related to #825
They are not the same problem but seem to be dual to each other.