0

Is it possible to understand why, and fix the faulty highlighting (inconsistent bold of only a few) in the listlistings for bash? The default rendering is worse than random. There is no reason why cd is bold and rm is not.

enter image description here

\begin{lstlisting}[
 basicstyle=\normalsize, language=Bash, showstringspaces=false, label={bashge2}, breaklines, caption=demo.sh
]
sudo ./compromise.sh # install a compromised tcc
cd /tmp
rm -rf tcctmp
\end{lstlisting}
2
  • 1
    Welcome to TeX.SX! Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem.
    – Thruston
    Oct 30, 2022 at 17:07
  • 1
    I agree that it looks poor, but technically I think sudo and rm are separate programs and not bash commands, which is why I guess the bash language definition leaves them out. You can see what is included in the docs here: ctan.org/pkg/listings, and it is really easy to add your own.
    – Thruston
    Oct 30, 2022 at 17:13

1 Answer 1

1

This is one way to improve it -- add some missing keywords, use some colour, and put it all in mono font.

\documentclass[border=5mm]{standalone}
\usepackage{listings}
\RequirePackage{xcolor}
\definecolor{textblue}{rgb}{.2,.2,.7}
\definecolor{textred}{rgb}{0.54,0,0}
\newcommand\basher{\lstset{
language=Bash,
basicstyle=\ttfamily,
keywordstyle=\color{textblue},
commentstyle=\color{textred},
frame=none,
columns=fullflexible,
keepspaces=true,
xleftmargin=\parindent,
showstringspaces=false,
morekeywords={rm, sudo}
}}
\lstnewenvironment{shell}[1][]{\basher\lstset{#1}}{}
\begin{document}
\begin{shell}
sudo ./compromise.sh # install a compromised tcc
cd /tmp
rm -rf tcctmp
\end{shell}
\end{document}

which should produce something like this:

enter image description here

The optional argument I have added to my suggested shell environment allows you to put the caption and label in like this:

\begin{shell}[caption=demo.sh, label={bashge2}]
sudo ./compromise.sh # install a compromised tcc
cd /tmp
rm -rf tcctmp
\end{shell}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .