4

I'm formatting SQL code in my document using minted:

\usepackage{minted}
\newmintedfile[sql]{sql}{breaklines=true,samepage,fontsize=\footnotesize}

% ...

\sql{query.sql}

In some of these files I'm using source as the name of a column, but minted recognises it as a keyword. In my SQL dialect this is fine though.

Is there a simple way I can disable source as a keyword in minted?


MWE:

\documentclass[10pt]{article}

\usepackage{minted}
\newmintedfile[sql]{sql}{breaklines=true,tabsize=4,samepage,frame=none,fontsize=\footnotesize}

\begin{document}

\sql{query.sql}

\end{document}

And the SQL for in query.sql:

SELECT 
    schedule,
    cost
FROM 
    Schedule
WHERE
        source = 'x'
    AND
        destination = 'y';
  • 3
    I'm not sure it's possible to change this on the TeX side, because minted delegates everything to Pygments. – egreg Mar 12 '15 at 11:11
4

It's possible to patch the Pygments macros to do what you want. The macros are of the form \PYG<style>{<token type>}{<code>} (Pygments token types). For the default style, this is \PYGdefault. For the patch to work, you need to set the style before patching, so that the style macros exist. Pygments is treating source as a keyword, which would be \PYGdefault{k}{source}. The patch detects the literal text source, and switches from keyword (k) to name (n).

Note that this only works with minted 2.0+ (which you are using, since you have the breaklines option).

\documentclass{article}

\usepackage{minted}

\usemintedstyle{default}
\AtBeginEnvironment{minted}{%
  \let\originalPYGdefault\PYGdefault
  \renewcommand{\PYGdefault}[2]{%
    \ifstrequal{#1}{k}%
     {\ifstrequal{#2}{source}%
       {\originalPYGdefault{n}{#2}}%
       {\originalPYGdefault{#1}{#2}}}%
     {\originalPYGdefault{#1}{#2}}%
  }%
}

\begin{document}

\begin{minted}[breaklines=true,tabsize=4,samepage,frame=none,fontsize=\footnotesize]{sql}
SELECT 
    schedule,
    cost
FROM 
    Schedule
WHERE
        source = 'x'
    AND
        destination = 'y';
\end{minted}

\end{document}
  • Thanks a lot! I found that by removing \AtBeginEnvironment I can still use the \sql{} command I had initially (which keeps the rest of the code cleaner). Is that somehow bad practice? – Keelan Mar 12 '15 at 19:15
  • 1
    @CamilStaps The \AtBeginEnvironment is just to limit the scope of the redefinitions. Removing it shouldn't usually cause any problems, and as you found, the definitions need to be more broadly available for your custom command. – G. Poore Mar 12 '15 at 21:52

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.