Cor de fundo em listagens de código embutidas com quebra de linha

Cor de fundo em listagens de código embutidas com quebra de linha

Quero destacar o código embutido com um fundo cinza. De preferência com listings. Esta questão está relacionadaFundo colorido em listagens inline.

Mas, como mostra meu MWE, não funciona com quebras de linha. Existe uma maneira de corrigir isso? Também tentei o pacote soul, mas produz muitos erros (comentários no 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}

Responder1

O problema é que \colorbox{}é uma caixa, o que significa que você não pode ter uma nova linha dentro dela.

Você pode tentar usar \parboxmas isso também não fará o que você deseja (veja \shellthree).

Você pode resolver seu problema com package soule \textttconforme mostrado nas últimas três linhas do seguinte 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}

Eu uso package soul, mudo a cor com \colorlet, defino a cor com \sethlcolore uso texthl{texttt{}}para definir o texto. Isso funciona também para quebras de linha.

Responder2

Você pode combinar lua-el(para destacar linhas) e piton(para coloração sintática de listagens informáticas). Ambos os pacotes requerem uma compilação com 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}

Saída do código acima

informação relacionada