
我想用灰色背景突出顯示內聯代碼。最好與listings
.這個問題與內嵌清單中的彩色背景。
但正如我的 MWE 所示,它不適用於換行符。有沒有辦法解決這個問題?我也嘗試了靈魂包,但這會產生很多錯誤(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
)。
您可以使用 package 來解決您的問題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}