3

I would like to write some code inline with other text. The code should match other code I write in the document though. So it should be highlighted (let's say yellow), should be in typewriter font and should not need any escaping of special characters. Also, it should be able to break at the end of the line. I can do the individual things but cannot combine them together.

This post seems to do similar things and is where I got the idea to use the soul package but didn't solve the problem of escaping special characters and just putting it into \verb doesn't work: Colorbox does not linebreak

MWE:

\documentclass{minimal}  
\usepackage {color}  
\usepackage {soul}  
\usepackage {listings}  
\lstset{breaklines=true, basicstyle=\ttfamily}


\begin{document}


text \colorbox{yellow}{\texttt{Code which cannot break lines and needs special characters escaped \_ bla bla bla very long line of code}} text

\sethlcolor{yellow}  
text \texttt{\hl{Code which can break very long lines and is highlighted but needs escaping of special characters \_ }} text

text \lstinline[language={}]{Code which can break lines of very long code and can escape special characters such as \ and & but cannot be highlighted} text

\end{document}
Clément
  • 5,591
ask_ask
  • 35

1 Answers1

2

This is indeed not easy to achieve. The best way that I found was to use the minted package, which requires to compile your document with the --shell-escape option (look at this question to learn how to automate this process).

The following document:

% !TeX TXS-program:compile = txs:///pdflatex/[--shell-escape]
%  Need to be compiled with the --shell-escape option!
% E.g., pdflatex --shell-escape test.tex 

\documentclass{article}
\usepackage{minted}
\usepackage{soul}

\begin{document}

\begin{minted}[%
    escapeinside=||, % Anything between those symbols will be interpreted as a TeX command.
    breaklines, % We allow lines to be broken.
    breaksymbolleft = 
        { \tiny \ensuremath{\hookrightarrow} }% Symbol used when a line is broken. We use the default.
    ] % We want to format simple text, but you can have programing languages too.
    {text}
Code which can break, does not need escaping (&, _) and can be highlighted using \hl : |\hl{test}| Lines are automatically broken!
\end{minted}

\end{document}

produces:

enter image description here

You can tune which font minted use, using the fontfamily option (look in their documentation, p. 25). But, by default, the tt font is used, so that should be consistent with the rest of your document.

One limitation is that the highlighted text will cause minted to crash if it needs to be wrapped: you need to take care of that "by hand".

Clément
  • 5,591
  • This looks very promising. Thank you. I will try it out and read the documentation. I am working on a linux machine and use vim and vimtex to compile but I hope that I can make it work. – ask_ask Jan 15 '20 at 16:43
  • @ask_ask Just try and compile it with vimtex. It should understand automatically the magic comment at the beginning, that asks to use shell-escape. – Clément Jan 15 '20 at 16:48