為單一清單評論分隔符號著色(shell 腳本語言樣式)?

為單一清單評論分隔符號著色(shell 腳本語言樣式)?

我希望在清單中有一種 shell 轉錄語言風格,其中強調 shell 提示符和實際命令,而文字的其餘部分有點「靜音」。

我現在能做到的最好的方法是將基本樣式定義為“靜音”/“褪色”/“灰色”,然後將我的 shell 提示字元(此處$)視為單個註釋分隔符,然後將剩餘的註解採用全黑色樣式並加粗:

\documentclass{article}
\usepackage{listings}
\usepackage{xcolor}

\lstdefinelanguage{ShellTranscript}
{sensitive=false,
morecomment=[l][\color{black}\bfseries]{ \$ },
basicstyle=\ttfamily\footnotesize\color{black!70},
}

\begin{document}

Here we go:

\begin{lstlisting}[language=ShellTranscript]

user@box:~ $ ls /usr/
bin  games  include  lib  local  man  sbin  share  src
user@box:~ $ ls /var/
backups  cache  lib  local  lock  log  mail  opt  run  spool  swap  tmp
user@box:~ $ echo 'be careful of $ in there!'
be careful of $ in there!
\end{lstlisting}

\end{document}

輸出是這樣的:

輸出

然而,我真正想要的是:

期望輸出

即:只有註解分隔符號(即只是提示字元$)應該是紅色且非粗體;之後的文字(實際命令)應為黑色且粗體;剩餘的文字(如輸出)應該是靜音/灰色的 - 並且$在“輸出文字”中不應設置樣式(這可能意味著為了聲明“註釋”,必須檢查該行是否以 開頭user@box並包含 a $,然後如果它確實如此,將其$作為註釋分隔符)。

這可以透過列表來實現嗎?

答案1

您可以改編@matexmatics的解決方案https://tex.stackexchange.com/a/706145/36296像這樣:

\documentclass{article}
\usepackage{listings}
\usepackage{xcolor}
\usepackage[T1]{fontenc}
\usepackage{beramono}
\colorlet{dollarcol}{red}

\lstdefinelanguage{ShellTranscript}
{sensitive=false,
basicstyle=\ttfamily\footnotesize\color{black!30},
moredelim=[s][\colorlet{dollarcol}{red}]{user@box:~},
moredelim=**[il][\textcolor{dollarcol}{\$}\color{black}\bfseries\colorlet{dollarcol}{black}]{\$}
}

\begin{document}

Here we go:

\begin{lstlisting}[language=ShellTranscript]

user@box:~ $ ls /usr/
bin  games  include  lib  local  man  sbin  share  src
user@box:~ $ ls /var/
backups  cache  lib  local  lock  log  mail  opt  run  spool  swap  tmp
user@box:~ $ echo 'be careful of $ in there!'
be careful of $ in there!
\end{lstlisting}

\end{document}

在此輸入影像描述

相關內容