Search code examples
latexreportdocumentationpdflatexlistings

Problem with embedding a Bash code in LaTeX


I am trying to use listings package in order to embed this bash snippet into my LaTeX report.

┌──(kali㉿kali)-[~]
└─$ passwd kali
Changing password for kali.
Current password:
New password:
Retype new password:
passwd: password updated successfully

┌──(kali㉿kali)-[~]
└─$

1- It seems I cannot use special characters bellow:

    ┌──(kali㉿kali)-[~]
    └─$

And I receive the following error: ! Package inputenc Error: Invalid UTF-8 byte sequence. although I have used: extendedchars=true

2- I cannot bold the prompt line, although I use: \%*\textbf{$ passwd kali}*)

Here is my code in brief:

\usepackage{color}
\definecolor{verylightgray}{rgb}{0.9,0.9,0.9}
\usepackage{listings}
\lstset{
    basicstyle=\footnotesize,
    frame=single,
    backgroundcolor=\color{verylightgray},
    extendedchars=true
}
...

\begin{lstlisting}[numbers=left]

\%*\textbf{$ passwd kali}*)
Changing password for kali.
Current password:
New password:
Retype new password:
passwd: password updated successfully

\end{lstlisting}

Solution

    • If you want to use formatting commands like \textbf{...} you must use it within escape chars, e.g. choose @ as escape char with escapechar={@}

    • listings as such does not support unicode, but if you place all the special characters also in escape characters, you can use an unicode aware engine like lualatex or xelatex to compile your document.

    • you will also need to find a font which support exotic characters like . You can use the command line tool albatross for this. If you call albatross ㉿ it will give you a list of all your fonts that support the character:

            __ __           __
    .---.-.|  |  |--.---.-.|  |_.----.-----.-----.-----.
    |  _  ||  |  _  |  _  ||   _|   _|  _  |__ --|__ --|
    |___._||__|_____|___._||____|__| |_____|_____|_____|
    
                         Unicode code point 327F mapping to ㉿                     
    ┌─────────────────────────────────────────────────────────────────────────────┐
    │ Font name                                                                          │
    ├─────────────────────────────────────────────────────────────────────────────┤
    │ Adobe Myungjo Std,Adobe 명조 Std,Adobe Myungjo Std M,Adobe 명조 Std M                │
    ├─────────────────────────────────────────────────────────────────────────────┤
    │ Apple SD Gothic Neo,Apple SD 산돌고딕 Neo                                            │
    ├─────────────────────────────────────────────────────────────────────────────┤
    │ AppleGothic                                                                        │
    ├─────────────────────────────────────────────────────────────────────────────┤
    │ AppleMyungjo                                                                       │
    ├─────────────────────────────────────────────────────────────────────────────┤
    │ Arial Unicode MS                                                                   │
    ├─────────────────────────────────────────────────────────────────────────────┤
    │ Batang,바탕                                                                         │
    ├─────────────────────────────────────────────────────────────────────────────┤
    │ Gulim,굴림                                                                          │
    ├─────────────────────────────────────────────────────────────────────────────┤
    │ GungSeo                                                                            │
    ├─────────────────────────────────────────────────────────────────────────────┤
    │ LastResort                                                                         │
    ├─────────────────────────────────────────────────────────────────────────────┤
    │ Nanum Brush Script,나눔손글씨 붓                                                      │
    ├─────────────────────────────────────────────────────────────────────────────┤
    │ Nanum Gothic,나눔고딕                                                                │
    ├─────────────────────────────────────────────────────────────────────────────┤
    │ Nanum Gothic,나눔고딕,NanumGothic ExtraBold,나눔고딕 ExtraBold                         │
    ├─────────────────────────────────────────────────────────────────────────────┤
    │ Nanum Myeongjo,나눔명조                                                              │
    ├─────────────────────────────────────────────────────────────────────────────┤
    │ Nanum Myeongjo,나눔명조,NanumMyeongjoExtraBold,나눔명조 ExtraBold                      │
    ├─────────────────────────────────────────────────────────────────────────────┤
    │ Nanum Pen Script,나눔손글씨 펜                                                        │
    ├─────────────────────────────────────────────────────────────────────────────┤
    │ PCMyungjo                                                                          │
    ├─────────────────────────────────────────────────────────────────────────────┤
    │ PilGi                                                                              │
    

    % !TeX TS-program = lualatex
    
    \documentclass{article}
    
    \usepackage{fontspec}
    
    \setmonofont{Arial Unicode MS}
    
    \usepackage{color}
    \definecolor{verylightgray}{rgb}{0.9,0.9,0.9}
    \usepackage{listings}
    
    \lstset{
        basicstyle=\footnotesize\ttfamily,
        frame=single,
        backgroundcolor=\color{verylightgray},
        extendedchars=true,
        escapechar={@},
    }
    
    
    \begin{document}
    
    \begin{lstlisting}
    @\fontspec{Arial}\selectfont\textbf{\$ passwd kali}@
    
    @┌──@(kali@㉿@kali)-[~]
    @└─@$ passwd kali
    Changing password for kali.
    Current password:
    New password:
    Retype new password:
    passwd: password updated successfully
    
    @┌──@(kali@㉿@kali)-[~]
    @└─@$
    \end{lstlisting}
    
    \end{document}
    

    enter image description here