4

I write my document in other language, then I need to switch to English (switch font) when show code:

\newcommand\code[1]{\lstinline{#1}}
\pretocmd{\code}{\begin{otherlanguage}{english}}{}{}
\apptocmd{\code}{\end{otherlanguage}}{}{}

Then I face a problem when I try to put a special character into my defined command, like this:

\code{HEAD^^}

Since I knew this would be fine within those command:

\lstinline{HEAD^^} ...or... \lstinline|HEAD^^|

So I want to undefined my command and use the original, but this seems not work:

\pretocmd{\lstlisting}{\begin{otherlanguage}{english}}{}{}

How can I use \pretocmd with it?

1
  • 2
    \lstinline uses several steps in order not to absorb its argument too early and so to be able to print special characters, so defining \code with an argument defeats this special handling.
    – egreg
    Jul 15, 2014 at 23:16

1 Answer 1

3

Ok, I didn't find the answer to my question, but this workaround is just working:

\lstset{basicstyle=\selectlanguage{english}\ttfamily}

This switch my language (and font) to English everywhere I call \lstinline and \begin{lstlisting} ... \end{lstlisting}, and switch back to my main language when the scope end. So I no longer need those \pretocmd or \BeforeBeginEnvironment anymore.

You must log in to answer this question.

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