2

In the small document listed below, I try to include some danish special chars in a lstlisting environment, but that does not work. However it works fine in the verbatim environment. I use the pdflatex command to compile the document. If I change it to xelatex it works.

\documentclass{article}
\usepackage{listings}

\begin{document}
Some text

\begin{verbatim}
danish special chars æ ø å
\end{verbatim}

\begin{lstlisting}
danish special chars æ ø å
\end{lstlisting}

\end{document}

The logfile from running pdflatex on the file is shown below.

This is pdfTeX, Version 3.14159265-2.6-1.40.21 (TeX Live 2020) (preloaded format=pdflatex 2020.10.13)  5 JAN 2021 12:48
entering extended mode
 restricted \write18 enabled.
 %&-line parsing enabled.
**listingsissue.tex --shell-escape
(./listingsissue.tex
LaTeX2e <2020-10-01> patch level 1
L3 programming layer <2020-10-05> xparse <2020-03-03>
(/home/henrik/.TinyTeX/texmf-dist/tex/latex/base/article.cls
Document Class: article 2020/04/10 v1.4m Standard LaTeX document class
(/home/henrik/.TinyTeX/texmf-dist/tex/latex/base/size10.clo
File: size10.clo 2020/04/10 v1.4m Standard LaTeX file (size option)
)
\c@part=\count175
\c@section=\count176
\c@subsection=\count177
\c@subsubsection=\count178
\c@paragraph=\count179
\c@subparagraph=\count180
\c@figure=\count181
\c@table=\count182
\abovecaptionskip=\skip47
\belowcaptionskip=\skip48
\bibindent=\dimen138
)
(/home/henrik/.TinyTeX/texmf-dist/tex/latex/listings/listings.sty
(/home/henrik/.TinyTeX/texmf-dist/tex/latex/graphics/keyval.sty
Package: keyval 2014/10/28 v1.15 key=value parser (DPC)
\KV@toks@=\toks15
)
\lst@mode=\count183
\lst@gtempboxa=\box47
\lst@token=\toks16
\lst@length=\count184
\lst@currlwidth=\dimen139
\lst@column=\count185
\lst@pos=\count186
\lst@lostspace=\dimen140
\lst@width=\dimen141
\lst@newlines=\count187
\lst@lineno=\count188
\lst@maxwidth=\dimen142

(/home/henrik/.TinyTeX/texmf-dist/tex/latex/listings/lstmisc.sty
File: lstmisc.sty 2020/03/24 1.8d (Carsten Heinz)
\c@lstnumber=\count189
\lst@skipnumbers=\count190
\lst@framebox=\box48
)
(/home/henrik/.TinyTeX/texmf-dist/tex/latex/listings/listings.cfg
File: listings.cfg 2020/03/24 1.8d listings configuration
))
Package: listings 2020/03/24 1.8d (Carsten Heinz)

(/home/henrik/.TinyTeX/texmf-dist/tex/latex/l3backend/l3backend-pdftex.def
File: l3backend-pdftex.def 2020-09-24 L3 backend support: PDF output (pdfTeX)
\l__kernel_color_stack_int=\count191
\l__pdf_internal_box=\box49
)
No file listingsissue.aux.
\openout1 = `listingsissue.aux'.

LaTeX Font Info:    Checking defaults for OML/cmm/m/it on input line 4.
LaTeX Font Info:    ... okay on input line 4.
LaTeX Font Info:    Checking defaults for OMS/cmsy/m/n on input line 4.
LaTeX Font Info:    ... okay on input line 4.
LaTeX Font Info:    Checking defaults for OT1/cmr/m/n on input line 4.
LaTeX Font Info:    ... okay on input line 4.
LaTeX Font Info:    Checking defaults for T1/cmr/m/n on input line 4.
LaTeX Font Info:    ... okay on input line 4.
LaTeX Font Info:    Checking defaults for TS1/cmr/m/n on input line 4.
LaTeX Font Info:    ... okay on input line 4.
LaTeX Font Info:    Checking defaults for OMX/cmex/m/n on input line 4.
LaTeX Font Info:    ... okay on input line 4.
LaTeX Font Info:    Checking defaults for U/cmr/m/n on input line 4.
LaTeX Font Info:    ... okay on input line 4.
\c@lstlisting=\count192

! Package inputenc Error: Invalid UTF-8 byte sequence.

See the inputenc package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              
                                                  
l.12 danish special chars æ 
                             ø å
? R
OK, entering \nonstopmode...

! Package inputenc Error: Invalid UTF-8 byte "A6.

See the inputenc package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              
                                                  
l.12 danish special chars æ 
                             ø å
The document does not appear to be in UTF-8 encoding.
Try adding \UseRawInputEncoding as the first line of the file
or specify an encoding such as \usepackage [latin1]{inputenc}
in the document preamble.
Alternatively, save the file in UTF-8 using your editor or another tool


! Package inputenc Error: Invalid UTF-8 byte sequence.

See the inputenc package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              
                                                  
l.12 danish special chars æ ø 
                                å
The document does not appear to be in UTF-8 encoding.
Try adding \UseRawInputEncoding as the first line of the file
or specify an encoding such as \usepackage [latin1]{inputenc}
in the document preamble.
Alternatively, save the file in UTF-8 using your editor or another tool


! Package inputenc Error: Invalid UTF-8 byte "B8.

See the inputenc package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              
                                                  
l.12 danish special chars æ ø 
                                å
The document does not appear to be in UTF-8 encoding.
Try adding \UseRawInputEncoding as the first line of the file
or specify an encoding such as \usepackage [latin1]{inputenc}
in the document preamble.
Alternatively, save the file in UTF-8 using your editor or another tool


! Package inputenc Error: Invalid UTF-8 byte sequence.

See the inputenc package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              
                                                  
l.12 danish special chars æ ø å
                                  
The document does not appear to be in UTF-8 encoding.
Try adding \UseRawInputEncoding as the first line of the file
or specify an encoding such as \usepackage [latin1]{inputenc}
in the document preamble.
Alternatively, save the file in UTF-8 using your editor or another tool


! Package inputenc Error: Invalid UTF-8 byte "A5.

See the inputenc package documentation for explanation.
Type  H <return>  for immediate help.
 ...                                              
                                                  
l.12 danish special chars æ ø å
                                  
The document does not appear to be in UTF-8 encoding.
Try adding \UseRawInputEncoding as the first line of the file
or specify an encoding such as \usepackage [latin1]{inputenc}
in the document preamble.
Alternatively, save the file in UTF-8 using your editor or another tool

[1

{/home/henrik/.TinyTeX/texmf-var/fonts/map/pdftex/updmap/pdftex.map}]
(./listingsissue.aux) ) 
Here is how much of TeX's memory you used:
 2021 strings out of 481861
 28560 string characters out of 5930866
 286420 words of memory out of 5000000
 18407 multiletter control sequences out of 15000+600000
 535256 words of font info for 30 fonts, out of 8000000 for 9000
 14 hyphenation exceptions out of 8191
 47i,4n,55p,175b,976s stack positions out of 5000i,500n,10000p,200000b,80000s
</home/henrik/.TinyTeX/texmf-dist/fonts/type1/public/ams
fonts/cm/cmr10.pfb></home/henrik/.TinyTeX/texmf-dist/fonts/type1/public/amsfont
s/cm/cmtt10.pfb>
Output written on listingsissue.pdf (1 page, 24642 bytes).
PDF statistics:
 16 PDF objects out of 1000 (max. 8388607)
 10 compressed objects within 1 object stream
 0 named destinations out of 1000 (max. 500000)
 1 words of extra memory for PDF output out of 10000 (max. 10000000)
1

1 Answer 1

3

If you want to stick with pdflatex, this should work:

\documentclass{article}
\usepackage{listings}

\begin{document}
\noindent danish special chars æ ø å

\lstset{literate=%
{æ}{{\ae}}1
{ø}{{\o}}1
{å}{{\aa}}1
}

\begin{lstlisting}
danish special chars æ ø å
\end{lstlisting}

\end{document}

enter image description here

1
  • Fantastic. I did not know about the literate option for listings. A clean solution to my problem. Thanks.
    – midtiby
    Commented Jan 6, 2021 at 8:02

You must log in to answer this question.

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