0

I have come to a point where I really need a way to have LaTeX automatically break lines formatted as inline listings. I'm using the package minted. Here's a minimal working example:

\documentclass{article}

\usepackage{minted}
\usepackage{xcolor}
\usepackage{fontspec}

\definecolor{bg}{rgb}{0.85, 0.85, 0.85}
\setmintedinline{bgcolor=bg}
\setmonofont{Courier New}
\begin{document}
    Hello world
    
    \mintinline{text}{aVeryLongStringaVeryLongStringaVeryLongStringaVeryLongStringaVeryLongStringaVeryLongStringaVeryLongStringaVeryLongString}
    
    \Verb[breaklines, breakanywhere]{aVeryLongStringaVeryLongStringaVeryLongStringaVeryLongStringaVeryLongStringaVeryLongStringaVeryLongStringaVeryLongString}
\end{document}

When compiling with latexmk -lualatex -shell-escape test.tex, the result is as follows:

enter image description here

According to the docs for minted, breaklines and breakanywhere are not available for \mintinline. I'm not sure about breakbytoken, but I'd be open to using it if it could potentially fix my problem.

There are two main issues logged in the minted repository that attempt to address this problem:

I read through both issues and it seems that the only viable workaround for now is to use \Verb instead of \mintinline. However there are a couple of problems with \Verb as far as I can tell:

  • You cannot set a language (Python, C++) the same way you can with \mintinline
  • You cannot set a bgcolor, which I like to have to clearly distinguish inline code from surrounding text.

There has got to be a way to achieve this using \mintinline? Any suggestions or ideas?

1 Answer 1

3

You can do this in two steps:

The breaklines option does already work with \mintinline, but breakanywhere doesn't. If you look into fvextra at the implementation of this you can see that you can add this by adding \FancyVerbBreakStart and \FancyVerbBreakStop in \minted@inputpyg. We can patch this in with

\usepackage{etoolbox}

\makeatletter
% Patch minted to allow breakanywhere for \mintinline
\patchcmd \minted@inputpyg {%
  \FancyVerbFormatLine
}{%
  \FancyVerbFormatLine{%
    \strut\FancyVerbFormatText{\FancyVerbBreakStart##1\FancyVerbBreakStop}\strut%
  }%
  \@gobble
}{}{\failure}%
\makeatother

Additionally bgcolor is implemented as a colorbox and therefore is incompatible with linebreaking. You can avoid this by applying the background color as a highlight with lua-ul instead. Then you get the document

\documentclass{article}

\usepackage{minted}
\usepackage{xcolor,luacolor}
\usepackage{fontspec}
\usepackage{lua-ul}
\usepackage{etoolbox}

\definecolor{bg}{rgb}{0.85, 0.85, 0.85}

\makeatletter
% Patch minted to allow breakanywhere for mininline
\patchcmd \minted@inputpyg {%
  \FancyVerbFormatLine
}{%
  \FancyVerbFormatLine{%
    % \FV@BreakByTokenAnywhereHook
    \strut\FancyVerbFormatText{\FancyVerbBreakStart##1\FancyVerbBreakStop}\strut%
  }%
  \@gobble
}{}{\failure}%

% Wrap \mintinline to apply color through highlighting
\NewDocumentCommand\specialMintinline{O{} m v} {
  \highLight[bg]{%
    \mintinline[breaklines,breakanywhere,#1]{#2}{#3}%
  }%
}
\makeatother

\setmonofont{Courier New}
\begin{document}
    Hello world
    
    \specialMintinline{text}{aVeryLongStringaVeryLongStringaVeryLongStringaVeryLongStringaVeryLongStringaVeryLongStringaVeryLongStringaVeryLongString}
\end{document}

enter image description here

4
  • Is it possible to get rid of the inverted L character at the end of each line? I tried resetting breaksymbolright but that didn't work...
    – user32882
    Sep 5, 2022 at 4:38
  • You have to reset breakanywheresymbolpre, but be advised that then you can't determine anymore if there should be spaces between lines based on the output. Sep 5, 2022 at 6:18
  • Ok, I modified your code to \mintinline[breakanywheresymbolpre=,breaklines,breakanywhere,#1]{#2}{#3}% and that seems to work OK. Not sure what you mean by determination of spaces between lines. Are you referring to vertical space or horizontal space?
    – user32882
    Sep 5, 2022 at 7:12
  • 2
    @user32882 If one line ends with foo and the next one starts with bar, then you don't know if the original text was foobar or foo bar. Sep 5, 2022 at 18:03

You must log in to answer this question.

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