3

How I can display properly polish characters in code of programming language using listings package and lualatex compiler?

main.cpp:

//Zażółć gęślą jaźń

#include <iostream>

int main() { std::cout << "Hello World!\n"; return 0; }

main.tex:

\documentclass[12pt, a4paper]{article}

\usepackage[polish]{babel} \babelprovide[transforms = oneletter.nobreak]{polish}

\usepackage{listings} \usepackage{xcolor}

\definecolor{codegreen}{rgb}{0,0.6,0} \definecolor{codegray}{rgb}{0.5,0.5,0.5} \definecolor{codepurple}{rgb}{0.58,0,0.82} \definecolor{backcolour}{rgb}{0.95,0.95,0.92}

\lstdefinestyle{mystyle}{ backgroundcolor=\color{backcolour},
commentstyle=\color{codegreen}, keywordstyle=\color{magenta}, numberstyle=\tiny\color{codegray}, stringstyle=\color{codepurple}, basicstyle=\ttfamily\footnotesize, breakatwhitespace=false,
breaklines=true,
captionpos=b,
keepspaces=true,
numbers=left,
numbersep=5pt,
showspaces=false,
showstringspaces=false, showtabs=false,
extendedchars=true, tabsize=2 }

\lstset{style=mystyle}

\hbadness=99999

\begin{document}

\lstinputlisting[language=C++]{main.cpp}

\end{document}

First result (lualatex):

lualatex

My goal is to have the result, as the picture below (in lualatex compile):

goal

Many thanks in advance!

David Carlisle
  • 757,742

2 Answers2

5

A few bugs here.

  1. In pdflatex the transforms = oneletter.nobreak babel option is not supported. So you must use lualatex (or remove that feature)
  2. In lualatex do not use fontenc or inputenc. (by the way you don't have to use inputenc in newer versions Is there any reason to use inputenc?) In this particular case you must remove fontenc for it to work. (alternatively add both fontenc and literate works, but why bother?)
  3. listings has some bugs that doesn't allow it to handle characters with codepoint > 255. This half of the question is duplicate of https://tex.stackexchange.com/a/25396/250119. Use the fix there
\documentclass[12pt, a4paper]{article}

\usepackage[polish]{babel} \babelprovide[transforms = oneletter.nobreak]{polish}

\usepackage{listings} \usepackage{xcolor}

\makeatletter \lst@InputCatcodes \def\lst@DefEC{% \lst@CCECUse \lst@ProcessLetter ^^80^^81^^82^^83^^84^^85^^86^^87^^88^^89^^8a^^8b^^8c^^8d^^8e^^8f% ^^90^^91^^92^^93^^94^^95^^96^^97^^98^^99^^9a^^9b^^9c^^9d^^9e^^9f% ^^a0^^a1^^a2^^a3^^a4^^a5^^a6^^a7^^a8^^a9^^aa^^ab^^ac^^ad^^ae^^af% ^^b0^^b1^^b2^^b3^^b4^^b5^^b6^^b7^^b8^^b9^^ba^^bb^^bc^^bd^^be^^bf% ^^c0^^c1^^c2^^c3^^c4^^c5^^c6^^c7^^c8^^c9^^ca^^cb^^cc^^cd^^ce^^cf% ^^d0^^d1^^d2^^d3^^d4^^d5^^d6^^d7^^d8^^d9^^da^^db^^dc^^dd^^de^^df% ^^e0^^e1^^e2^^e3^^e4^^e5^^e6^^e7^^e8^^e9^^ea^^eb^^ec^^ed^^ee^^ef% ^^f0^^f1^^f2^^f3^^f4^^f5^^f6^^f7^^f8^^f9^^fa^^fb^^fc^^fd^^fe^^ff% żłćęśąźń% ^^00} \lst@RestoreCatcodes \makeatother

\definecolor{codegreen}{rgb}{0,0.6,0} \definecolor{codegray}{rgb}{0.5,0.5,0.5} \definecolor{codepurple}{rgb}{0.58,0,0.82} \definecolor{backcolour}{rgb}{0.95,0.95,0.92}

\lstdefinestyle{mystyle}{ backgroundcolor=\color{backcolour},
commentstyle=\color{codegreen}, keywordstyle=\color{magenta}, numberstyle=\tiny\color{codegray}, stringstyle=\color{codepurple}, basicstyle=\ttfamily\footnotesize, breakatwhitespace=false,
breaklines=true,
captionpos=b,
keepspaces=true,
numbers=left,
numbersep=5pt,
showspaces=false,
showstringspaces=false, showtabs=false,
extendedchars=true, tabsize=2 }

\lstset{style=mystyle}

\hbadness=99999

\begin{document}

\noindent Przetestujmy możliwości języka C++, oraz wiszące litery na końcu zdania i coś tam jeszcze: \newline

\lstinputlisting[language=C++]{main.cpp}

\end{document}

Seems to work for me.

(as you can probably infer, include every character with codepoint > 255 once in addition to the existing. You can also use the ^^^^<4 digits> or ^^^^^^<6 digits> notation to specify by hex code.)

user202729
  • 7,143
2

Just for comparison. What we can do with OpTeX:

\fontfam[lm]
\hsize=10cm

\noindent Przetestujmy możliwości języka C++, oraz wiszące litery na końcu zdania i coś tam jeszcze:

\verbinput \hisyntax{C} (-) main.cpp

\bye

The result:

wipet
  • 74,238