Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I would like to use LaTeX package minted to colorize my c and Cuda code. I need to write comments in the code in czech. I use package inputenc with utf-8 and czech characters (ěščřžýáíé) work good in whole paper but minted block:

LaTeX source:

    \begin{minted}{c}
        double m[4];    // staticky alokované pole
    \end{minted}

Output pdf:

    double m[4];    // staticky alokovan\unhbox \voidb@x \bgroup \let \unhbox \voidb@x \

EDIT: I have tried XeTeX. minted works fine with XeTeX and czech characters. But assymptote doesn't work for me with XeTeX. asy command used on .asy file from XeTeX utilize 100% of my cpu but not generate any output in casual time.

EDIT: Example for pdflatex:

   \documentclass[a4paper, pt11]{article}
   \usepackage[utf8]{inputenc}
   \usepackage{fullpage}
   \usepackage[english, czech]{babel}

   % More symbols
   \usepackage{amsmath}
   \usepackage{amssymb}
   \usepackage{latexsym}


   \usepackage{graphicx}
   \usepackage{asymptote}
   \usepackage{minted}

   \begin{document}
   \begin{figure}[h]
    \begin{asy}
        import unicode;
        size(15cm);
        fill((0, 0)--(0.5, 0)--(0.5, 1)--(0, 1)--cycle, red);
        fill((0.5, 0)--(6, 0)--(6, 1)--(0.5, 1)--cycle, blue);
        fill((6, 0)--(32, 0)--(32, 1)--(6, 1)--cycle, green);
        draw((0.25, 1.1)--(0.25, 1.9));
        label("znaménko", (0.25,2), N);
        label("exponent", (3.25,1), N);
        label("mantisa", (18,1), N);
        for(int i=0; i < 64; ++i) {
            draw((0.5i, 0)--(0.5i + 0.5, 0)--(0.5i + 0.5, 1)--(0.5i, 1)--cycle);
        }
        label("$63$",(0.25,0),S);
        label("$52$",(5.75,0),S);
        label("$0$",(31.75,0),S);
    \end{asy}
    \caption{Uspořádání \emph{double} v paměti}
    \label{doublemem}
   \end{figure}

    \begin{minted}{c}
    double m[4];    // staticky alokované pole
    m[0] = 1;       // přímé nastavení první složky
    double *n = (double *) malloc(pocet_cisel * 4 * sizeof (double));   // dynamická alokace
    n[4 + 0] = 1;   // přímý přístup do první složky druhého čísla
    free(n);        // vyčištění paměti
   \end{minted}
   \end{document}
share|improve this question

migrated from stackoverflow.com Jun 3 '11 at 12:35

This question came from our site for professional and enthusiast programmers.

    
Did you try the listings-package? –  strauberry Jun 3 '11 at 11:47
    
Yes. I have used \lstset{language=C, inputencoding=utf8} and I have got error: Package inputenc Error: Unicode char \u8:�\expandafter not set up for use with LaTeX. –  snilard Jun 3 '11 at 12:32
    
@snilard: please post your minimal example. –  xport Jun 3 '11 at 13:29
add comment

4 Answers 4

Here's a patch for fancyvrb and minted. This should let minted handle whatever Unicode pdfTeX can handle. @KonradRudolph All of this comes down to making sure everything is processed by \detokenize before it is saved to file.

enter image description here

\documentclass[11pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[english, czech]{babel}

\usepackage{fullpage}
\usepackage{minted}

\makeatletter
\newcommand{\minted@write@detok}[1]{%
  \immediate\write\FV@OutFile{\detokenize{#1}}}%

\newcommand{\minted@FVB@VerbatimOut}[1]{%
  \@bsphack
  \begingroup
    \FV@UseKeyValues
    \FV@DefineWhiteSpace
    \def\FV@Space{\space}%
    \FV@DefineTabOut
    %\def\FV@ProcessLine{\immediate\write\FV@OutFile}% %Old, non-Unicode version
    \let\FV@ProcessLine\minted@write@detok %Patch for Unicode
    \immediate\openout\FV@OutFile #1\relax
    \let\FV@FontScanPrep\relax
%% DG/SR modification begin - May. 18, 1998 (to avoid problems with ligatures)
    \let\@noligs\relax
%% DG/SR modification end
    \FV@Scan}
    \let\FVB@VerbatimOut\minted@FVB@VerbatimOut

\renewcommand\minted@savecode[1]{
  \immediate\openout\minted@code\jobname.pyg
  \immediate\write\minted@code{\expandafter\detokenize\expandafter{#1}}%
  \immediate\closeout\minted@code}
\makeatother

\begin{document}

\begin{minted}{c}
double m[4];    // staticky alokované pole
m[0] = 1;       // prímé nastavení první složky
double *n = (double *) malloc(pocet_cisel * 4 * sizeof (double));   // dynamická alokace
n[4 + 0] = 1;   // prímý prístup do první složky druhého císla
free(n);        // vycištení pameti
\end{minted}

\mint{c}|double m[4];    // staticky alokované pole|

\end{document}

Edit 2013/07/30

Equivalent code is now incorporated into the minted development version on GitHub.

share|improve this answer
    
this works for me. +1 –  alvatar Feb 3 '13 at 11:59
    
As alone document this work's for me too. But it hasn't any efect in my thesis. I have absolutely no idea why. –  snilard Mar 24 '13 at 17:03
    
@snilard If you can come up with a minimal working example, please add another comment or otherwise let me know. Perhaps there's another package you are loading that can cause some sort of conflict. –  G. Poore Mar 25 '13 at 13:46
    
I am sorry. I did some mistake because It works fine now. Thank you. –  snilard Apr 11 '13 at 14:47
add comment

minted doesn’t fully support inputenc; using external tools in conjunction with Unicode in LaTeX is all but impossible.

You either need to use XeTeX or LuaTeX instead of plain LaTeX, or use another highlighter package instead of minted – but as you’ve seen yourself they have their own share of problems with Unicode.

Personally, I suggest you use XeTeX. It ships with every modern LaTeX system. You just need to use the xelatex or luatex command instead of pdflatex for typesetting.

share|improve this answer
add comment

In my windows7-64 + Python3 + French + 2013-04-30th, this worked for me :

  • python part :
    • install python 3.3 (32bit), and the following sub-projects :
      . easy_install, (necessary to install pygmentize after)
      . pygmentize,
    • to have pygmentize in my path, i had to copy pygmentize.exe and pygmentize-script.py :
      . from C:\Python33\Scripts
      . to C:\Python33
  • tex part :
    • install TexLive or Miketex (2.9+), if not done,
    • check you have xelatex in it, and modules "minted", and "polyglossia",
    • if already yes, maybe check you are up-to-date in those module version,
  • tex editor parametrizing (in texmaker example, may vary for others):
    • use texmaker v4.0.1+ (or >= 3.5.1) (32bit usb or full install version),
    • check its default file saving mode is "utf-8",
    • configure xelatex compilation as follow :
      xelatex -8bit -shell-escape -synctex=1 -interaction=nonstopmode %.tex
      "-8bit" is to replace "TAB" character per beautifull spaces
      "-shell-escape" I don't remember (see other posts)
    • set default "quick compilation" to xelatex+pdf : (indeed, xelatex will compil to .xvd, and only .pdf viewer seems possible)
    • use then "quickcompilation" to transform your .tex into a .pdf.
  • adapting your former ".tex" files :
    • remove {inputenc} , {babel} references from your old .tex,
    • add {polyglossia},
  • working .tex file :

    %Format: WML-LaTeX2e
    %This work for Mathematics+French characters+Minted usage
    %For more complex case
    \documentclass[10pt,a4paper,notitlepage]{article}
    \usepackage{amsmath,amssymb}
    \usepackage[T1]{fontenc}
    %\usepackage[ansinew]{inputenc} no more
    %\usepackage[utf8]{inputenc} no more  
    %\usepackage[english, french]{babel} no more
    %\usepackage{fr} no more
    \usepackage{a4}
    
    % and now with
    \usepackage{polyglossia}
    \usepackage{fancyhdr}
    \usepackage{listings}
    \usepackage{minted}
    
    \topmargin -1.5cm 
    \headheight 0cm 
    \headsep 0.8cm 
    \topskip 0cm 
    \textheight 28cm 
    \footskip 1.5cm 
    \textwidth 18cm 
    \oddsidemargin -1cm 
    \evensidemargin 0cm 
    
    \begin{document}
    
    \centerline{\fbox{\begin{minipage}{10cm}\textbf {
    \begin{center}
     Titre accentué
    \end{center}
    }
    \end{minipage}}}
    
    
    \noindent Essai script
    
    \begin{minted}[mathescape]{python}
    # des caractères français
    for j in range(1,10) :
        print("c'est la fête !")
    \end{minted}
    \end{document}
    
share|improve this answer
1  
Welcome to TeX.sx! I've formatted your message using better mark up. In general it's best to avoid <pre> and </br> tags. –  egreg Apr 30 '13 at 11:05
add comment

FWIW, this example works fine with ConTeXt MkII (which uses pdftex engine). Instead of the minted package, I am using vim module for ConTeXt, which is similar to minted but uses vim for highlighting the file.

\enableregime[utf-8]
\usemodule[vim]

\definevimtyping[C]

\starttext
\startC   
  double m[4];    // staticky alokované pole
\stopC
\stoptext

The output file is written correctly. Therefore, it is highlighted correctly by vim, and the resulting pdf is fine. This shows that correct handling of encoding is possible in pdftex and hence should be possible in LaTeX as well.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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