Hintergrundfarbe in Inline-Codeauflistungen mit Zeilenumbruch

Hintergrundfarbe in Inline-Codeauflistungen mit Zeilenumbruch

Ich möchte Code inline mit einem grauen Hintergrund hervorheben. Am besten mit listings. Diese Frage bezieht sich aufFarbiger Hintergrund in Inline-Auflistungen.

Aber wie mein MWE zeigt, funktioniert es nicht mit Zeilenumbrüchen. Gibt es eine Möglichkeit, das zu beheben? Ich habe auch das Soul-Paket probiert, aber das produziert eine Menge Fehler (Kommentare im 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}

Antwort1

Das Problem besteht darin, dass es \colorbox{}sich um eine Box handelt und darin kein Zeilenumbruch möglich ist.

Sie können versuchen, es zu verwenden \parbox, aber das führt auch nicht zu dem gewünschten Ergebnis (siehe \shellthree).

Sie können Ihr Problem mit dem Paket lösen soul, \textttwie in den letzten drei Zeilen im folgenden MWE gezeigt:

\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}

Ich verwende das Paket soul, ändere die Farbe mit \colorlet, lege die Farbe mit fest \sethlcolorund verwende , texthl{texttt{}}um den Text festzulegen. Das funktioniert auch für Zeilenumbrüche.

Antwort2

lua-elSie können (zum Hervorheben von Zeilen) und piton(zur syntaktischen Hervorhebung von Informatikauflistungen) kombinieren . Beide Pakete erfordern eine Kompilierung mit 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}

Ausgabe des obigen Codes

verwandte Informationen