2

Sorry for an ugly post, it's my first one & I have very little time. I have this tiny document on overleaf:

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[polish]{babel}
\usepackage{polski}
\usepackage{listings}
\begin{document}
\lstinputlisting{Test.m}

where "Test.m" is a Matlab-file saved in encoding ANSI. What is in this file is:

% Żółć  
% Ściąć  
% Źrebię 

And what I get from overleaf is this:

% ????  
% ?ci??   
% ?rebi?  

Changing encoding of file Test.m to UTF-8 doesn't change antything. Changing encoding of document to cp1250 makes it all worse. Please help.

EDIT: \usepackage{fontspec} made some progress: now I have all desired letters in each line, but the problematic ones go first, than the rest, f.e.: "Źęrebi" instead of "Źrebię".

  • Welcome to TeX.SX! Did you try with \usepackage{listingsutf8}? – egreg Jan 22 '17 at 17:36
1

You can use listingsutf8:

% Just for convenience of a self-contained example
\begin{filecontents*}{\jobname.m}
% àé
% Żółć
% Ściąć
% Źrebię
\end{filecontents*}

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage[polish]{babel}
\usepackage{polski}
\usepackage{listingsutf8}

\begin{document}

\lstinputlisting[inputencoding=utf8/latin2,language=Matlab]{\jobname.m}

\end{document}

enter image description here

0

(Overleaf converts all uploaded text files to UTF-8.)

XeLaTeX seems to be the surest bet here.

\documentclass{article}
\usepackage[polish]{babel}
\usepackage{polski}
\usepackage{listings}
\usepackage{fontspec}  %% <-- loads Latin Modern

\lstset{extendedchars}
\begingroup
  \catcode0=12 %
  \makeatletter
  \g@addto@macro\lst@DefEC{%
    \lst@CCECUse\lst@ProcessLetter
    łżąęć % *** add Unicode characters ***
    ^^00% end marker
  }%
\endgroup

\begin{document}
\lstinputlisting{Test.m}
\end{document}

fontspec will load the Latin Modern family of fonts; it has the glyphs for the accented characters. Or you can use \setmainfont, \setsansfont and \setmonofont to load other suitable fonts.

You can switch the engine to XeLaTeX on Overleaf by clicking on the Settings icon (gear icon on the upper right), then select "XeLaTeX" from the "LaTeX engine" drop list.

EDIT: Some accented characters are "swapped" around in the listings output; but they'll be rendered correctly in normal text (i.e. outside the listings). This is explained here: Problem with special characters in listings So you'll have to add some Unicode characters to the extendedchars list (code added above).


Alternative: using minted

As you can imagine, the above approach can get rather unwieldy. Instead of using listings (or listingsutf8), you might want to consider using the minted package with xelatex:

\documentclass{article}
\usepackage[polish]{babel}
\usepackage{polski}
\usepackage{minted}
\usepackage{fontspec}

\begin{document}

\inputminted{matlab}{Test.m}

\end{document} 

Just bear in mind that if you're compiling this on your own machine, you'll need to run pdflatex --shell-latex (or if you're using MikTeX, --enable-write18) and have Python installed for minted to work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.