Color de fondo en listados de código en línea con salto de línea

Color de fondo en listados de código en línea con salto de línea

Quiero resaltar el código en línea con un fondo gris. Preferiblemente conlistings . Esta pregunta está relacionada conFondo de color en listados en línea.

Pero como muestra mi MWE, no funciona con saltos de línea. ¿Hay alguna manera de solucionar esto? También probé el paquete soul, pero produce muchos errores (comentarios en 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}

Respuesta1

El problema es que \colorbox{}es un cuadro, eso significa que no puedes tener una nueva línea dentro.

Puedes intentar usarlo \parbox, pero eso tampoco hará lo que quieres (ver \shellthree).

Puede resolver su problema con el paquete souly \textttcomo se muestra en las últimas tres líneas del siguiente 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}

Utilizo el paquete soul, cambio el color con \colorlet, configuro el color \sethlcolory lo uso texthl{texttt{}}para configurar el texto. Esto también funciona para saltos de línea.

Respuesta2

Puede combinar lua-el(para resaltar líneas) y piton(para colorear sintácticamente listados informáticos). Ambos paquetes requieren una compilación con 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}

Salida del código anterior

información relacionada