1

This question already has an answer here:

I use listing typography C++ code, it performs very well when formatting LaTeX code, but it has very strange character output when used to typeset C++ code. Here is my WME:

\documentclass{standalone} 

\usepackage{tcolorbox,listings}
\usepackage{listings}
\usepackage{tikz}
\usepackage{framed}

\definecolor{background}{HTML}{EEEEEE}
\definecolor{comments}{HTML}{868686}
\definecolor{darkred}{RGB}{139,0,0}
\definecolor{darkblue}{RGB}{0,0,139}
\definecolor{chartreuse}{RGB}{127,255,0}
\definecolor{drakgreen}{RGB}{0,128,0}
\definecolor{lightgray}{RGB}{238,239,240}


\lstset{
language=C++,
keywordstyle=\color{darkblue},
basicstyle=\normalfont\ttfamily,
commentstyle=\color{comments}\ttfamily,
stringstyle=\rmfamily,
numbers=none,
numberstyle=\scriptsize,
numbersep=8pt,
showstringspaces=false,
breaklines=true,
frameround=ftff,
%frame=single,
captionpos=t,
belowcaptionskip=0em,
belowskip=0em
}
\lstdefinestyle{mystyle}{
language=C++,
extendedchars=true, 
breaklines=true,
breakatwhitespace=true,
basicstyle=\ttfamily,
numbers=none
}

\tcbuselibrary{listings,skins,breakable}
\newtcblisting{code}{
arc=0mm,
top=0mm,
bottom=0mm,
left=3mm,
right=0mm,
width=\textwidth,
boxrule=1pt,
%colback=blue!20,
colback=lightgray,
listing only,
listing options={style=mystyle},
breakable
}

\begin{document}
\begin{code}
#include <iostream>
#include <fstream>
#include <functional>
#include <chrono>

using namespace std;
using namespace std::chrono;

int fib(int n) {
    function<int (int,int,int)> iter;
    iter=[n,&iter](int a,int b,int i) {
        if(i>=n) {
            return a;
        }
        else {
            return iter(b,b+a,i+1);
        }
    };
    return iter(0,1,0);
}

uint64_t count = 0;

uint64_t fibonacci(int n)
{
    ++::count;
    if (::count - (::count >> 32 << 32) == 0)
        cout << ::count << "\n";
    //++::count;
    if (n == 1 || n == 2) {
        return 1;
    }
    else
        return fibonacci(n - 1) + fibonacci(n - 2);
}

\end{code}

\end{document}

enter image description here

Where is the problem?

No errors or warnings.The resulting list is:

(c.listing) [1

] (c.aux)

 *File List*
standalone.cls    2018/03/26 v1.3a Class to compile TeX sub-files standalone
shellesc.sty    2016/06/07 v0.02a unified shell escape interface for LaTeX
ifluatex.sty    2016/05/16 v1.4 Provides the ifluatex switch (HO)
   ifpdf.sty    2018/09/07 v3.3 Provides the ifpdf switch
 ifxetex.sty    2010/09/12 v0.6 Provides ifxetex conditional
 xkeyval.sty    2014/12/03 v2.7a package option processing (HA)
 xkeyval.tex    2014/12/03 v2.7a key=value parser (HA)
standalone.cfg    2018/03/26 v1.3a Default configuration file for 'standalone' 
class
 article.cls    2014/09/29 v1.4h Standard LaTeX document class
  size10.clo    2014/09/29 v1.4h Standard LaTeX file (size option)
tcolorbox.sty    2018/07/26 version 4.14 text color boxes
     pgf.sty    2015/08/07 v3.0.1a (rcs-revision 1.15)
  pgfrcs.sty    2015/08/07 v3.0.1a (rcs-revision 1.31)
everyshi.sty    2001/05/15 v3.00 EveryShipout Package (MS)
  pgfrcs.code.tex
 pgfcore.sty    2010/04/11 v3.0.1a (rcs-revision 1.7)
graphicx.sty    2017/06/01 v1.1a Enhanced LaTeX Graphics (DPC,SPQR)
graphics.sty    2017/06/25 v1.2c Standard LaTeX Graphics (DPC,SPQR)
    trig.sty    2016/01/03 v1.10 sin cos tan (DPC)
graphics.cfg    2016/06/04 v1.11 sample graphics configuration
   xetex.def    2017/06/24 v5.0h Graphics/color driver for xetex
  pgfsys.sty    2014/07/09 v3.0.1a (rcs-revision 1.48)
  pgfsys.code.tex
pgfsyssoftpath.code.tex    2013/09/09  (rcs-revision 1.9)
pgfsysprotocol.code.tex    2006/10/16  (rcs-revision 1.4)
  xcolor.sty    2016/05/11 v2.12 LaTeX color extensions (UK)
   color.cfg    2016/01/02 v1.6 sample color configuration
 pgfcore.code.tex
pgfcomp-version-0-65.sty    2007/07/03 v3.0.1a (rcs-revision 1.7)
pgfcomp-version-1-18.sty    2007/07/23 v3.0.1a (rcs-revision 1.1)
verbatim.sty    2014/10/28 v1.5q LaTeX2e package for verbatim enhancements
 environ.sty    2014/05/04 v0.3 A new way to define environments
trimspaces.sty    2009/09/17 v1.1 Trim spaces around a token list
etoolbox.sty    2018/08/19 v2.5f e-TeX tools for LaTeX (JAW)
listings.sty    2018/09/02 1.7 (Carsten Heinz)
 lstmisc.sty    2018/09/02 1.7 (Carsten Heinz)
listings.cfg    2018/09/02 1.7 listings configuration
    tikz.sty    2015/08/07 v3.0.1a (rcs-revision 1.151)
  pgffor.sty    2013/12/13 v3.0.1a (rcs-revision 1.25)
 pgfkeys.sty    
 pgfkeys.code.tex
 pgfmath.sty    
 pgfmath.code.tex
  pgffor.code.tex
    tikz.code.tex
  framed.sty    2011/10/22 v 0.96: framed or shaded text with page breaks
lstlang1.sty    2018/09/02 1.7 listings language file
lstlang1.sty    2018/09/02 1.7 listings language file
 lstmisc.sty    2018/09/02 1.7 (Carsten Heinz)
pdftexcmds.sty    2018/09/10 v0.29 Utility functions of pdfTeX for LuaTeX (HO)
infwarerr.sty    2016/05/16 v1.4 Providing info/warning/error messages (HO)
 ltxcmds.sty    2016/05/16 v1.23 LaTeX kernel commands for general use (HO)
       c.listing
 ***********

 ) 

marked as duplicate by TeXnician, Kurt, Phelype Oleinik, Community Nov 30 '18 at 14:04

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • I mean it produces a lot of ^ and I characters, which is not available in my C++ code.The first line of log file is:This is XeTeX, Version 3.14159265-2.6-0.99999 (MiKTeX 2.9.6880 64-bit) (preloaded format=xelatex 2018.11.12) 30 NOV 2018 10:20 entering extended mode **./c.tex (c.tex LaTeX2e <2018-04-01> patch level 5 – mcmxciv Nov 30 '18 at 2:34
  • 2
    The output of ^^I indicates the problem is related to tab characters in your code. I can't reproduce the problem, though, even if I replace the spaces in your example by tabs. Perhaps try adding literate={\^^I}{{}}4 to your \lstdefinestyle{mystyle} options. – siracusa Nov 30 '18 at 4:18
  • adding literate={\^^I}{{}}4 to \lstdefinestyle{mystyle} options,it reports:! Package pgfkeys Error: I do not know the key '/tcb/literate', to which you pa ssed '{\^^I}{{}}4', and I am going to ignore it. Perhaps you misspelled it. – mcmxciv Nov 30 '18 at 4:27
  • Make sure you really added it to \lstdefinestyle{mystyle} and not to \newtcblisting{code}. I also get that error when I add it to the latter – siracusa Nov 30 '18 at 5:28
  • \lstdefinestyle{mystyle}{ language=C++, extendedchars=true, breaklines=true, breakatwhitespace=true, basicstyle=\ttfamily, literate={\^^I}{{}}4, numbers=none } – mcmxciv Nov 30 '18 at 5:42

Browse other questions tagged or ask your own question.