Цвет фона в листингах встроенного кода с переносом строки

Цвет фона в листингах встроенного кода с переносом строки

Я хочу выделить встроенный код серым фоном. Предпочтительно с listings. Этот вопрос связан сЦветной фон в строковых списках.

Но как показывает мой MWE, он не работает с переносами строк. Есть ли способ это исправить? Я также пробовал пакет soul, но он выдает много ошибок (комментарии в MWE):

\documentclass{article}

\usepackage{listings}
\usepackage{xcolor}
\usepackage{soul}

\lstdefinestyle{ShellCmd}{
    language=bash,
    basicstyle=\small\ttfamily,
    backgroundcolor=\color{gray!10},
    linewidth=0.9\linewidth,
    breaklines=true
}
\def\ownstrut{\vrule height5.5pt depth0.8pt width0pt}

\newcommand\shellcmd[1]{\colorbox{gray!10}{\lstinline[style=ShellCmd,mathescape]`#1`}}
%\newcommand\shelltwo[1]{\hl{\lstinline[style=ShellCmd,mathescape]`#1`}} % this produces error

\begin{document}

\shellcmd{command}, some text \shellcmd{a very long command which should not destroy the whole layout when a line break occurs! Maybe it is possible to make even three or more lines}, and here it continues. 
%\shelltwo{a very long command which should not destroy the whole layout when a line break occurs! Maybe it is possible to make even three or more lines}

\end{document}

решение1

Проблема в том, что \colorbox{}это блок, а значит внутри него нельзя вставить новую строку.

Вы можете попробовать использовать \parbox, но это также не даст желаемого результата (см. \shellthree).

Вы можете решить свою проблему с помощью пакета soul, \textttкак показано в последних трех строках следующего MWE:

\documentclass{article}

\usepackage{listings}
\usepackage{xcolor,showframe}
\usepackage{soul}

\lstdefinestyle{ShellCmd}{
    language=bash,
    basicstyle=\small\ttfamily,
    backgroundcolor=\color{gray!10},
    linewidth=0.9\linewidth,
    breaklines=true
}

\newcommand\shellthree[1]{\colorbox{gray!10}{\parbox[t]{\textwidth}{\lstinline[style=ShellCmd,mathescape]`#1`}}}


\begin{document} 

\lstinline[{backgroundcolor=\color{gray!50}}]{Testzeile testzeile testzeile}
\lstinline[style=ShellCmd]{Testzeile testzeile testzeile}

Command some text \shellthree{a very long command which should not destroy the whole layout when a line break occurs! Maybe it is possible to make even three or more lines}, 
and here it continues. and here it continues and here it continues and here it continues 

\colorlet{hellgrau}{gray!10}
\sethlcolor{hellgrau}
Text Text Text Text Text Text Text Text Text Text Text Text \texthl{\texttt {text highlight with soul}}.

\end{document}

Я использую package soul, меняю цвет с помощью \colorlet, устанавливаю цвет с помощью \sethlcolorи использую texthl{texttt{}}для установки текста. Это работает также для переносов строк.

решение2

Вы можете комбинировать lua-el(для выделения строк) и piton(для синтаксической раскраски информационных листингов). Оба пакета требуют компиляции с LuaLaTeX.

\documentclass{article}
\usepackage{piton}
\usepackage{luacolor,lua-ul}
\LuaULSetHighLightColor{gray!15}

\usepackage{piton}

\begin{document}

\makeatletter
\newcommand\shellcmd[1]{{\@highLight\piton{#1}}}
\makeatother

\PitonOptions{break-lines-in-piton,language = minimal}

\shellcmd{command}, some text \shellcmd{a very long command which should not destroy the whole layout when a line
  break occurs! Maybe it is possible to make even three or more lines}, and here it continues.

\end{document}

Вывод кода выше

Связанный контент