我在自己的环境中使用 lstlisting 环境时出现奇怪的符号?

我在自己的环境中使用 lstlisting 环境时出现奇怪的符号?
\documentclass[openany]{book}
\raggedbottom       %reduce underfull \vbox

\usepackage{titlesec, titletoc}
\titlelabel{\thetitle\quad}
%change the part format in book 
\titleformat{\part}[frame]{\normalfont\huge\bfseries}{\partname\ \thepart }{20pt}{\Huge\centering}
%change the part format in table of contents
\titlecontents{part}%
[0pt]{\sffamily\bfseries\huge\protect\addvspace{25pt}\titlerule\addvspace{1.5ex}}%remove rule if you like
{}{\partname~}
{\hfill\contentspage}%replaced with {} if don't want page number for parts
[\addvspace{0.7ex}\titlerule\addvspace{10pt}]%remove rule if you like

%change the chapter format in table of contents
\titlecontents{chapter}%
[0pt]{\sffamily\bfseries\large\protect\addvspace{10pt}}%
{\chaptertitlename\ \thecontentslabel~}{}
{\hfill\contentspage}%
[\addvspace{1.5ex}]%

%change the appendix name from chapter to appendix
\makeatletter
\g@addto@macro{\appendix}{%
  \renewcommand\chaptername{Appendix}%
  \addtocontents{toc}{\protect\renewcommand*{\protect\chaptername}{\protect\appendixname}}%
}
\makeatother
%force paragraph newline
\titleformat{\paragraph}[hang]{\normalfont\normalsize\bfseries}{\theparagraph}{1em}{}


%insert figures
\usepackage{graphicx}

%improve the interface for defining floating objects such as figures and tables
\usepackage{float}

%customize page layout
\usepackage[a4paper, left=3cm, right=3cm]{geometry}

%color
\usepackage[dvipsnames]{xcolor}

%page headers and footers   left,right,odd,even
\usepackage{fancyhdr}
\pagestyle{fancy}
\fancyhf{}
\fancyhead[RE]{\normalfont\small\rmfamily\nouppercase{\leftmark}}
\fancyhead[LO]{\normalfont\small\rmfamily\nouppercase{\rightmark}}
\fancyhead[LE,RO]{\thepage}

%booktitle
\title{\Huge C++ \\\qquad\textcolor{RedOrange}{Primer}}
\author{\textbf{Stanley B.Lippman} \and \textbf{Jos\'ee Lajoie} \and \textbf{Barbara E.Moo}}
\date{}

%motto
\usepackage{epigraph}
\renewcommand{\epigraphflush}{center} %center
\setlength\epigraphrule{0pt} %cancel the line between words and person name

%code
\usepackage{listings}
\lstset{language=[Visual]C++,
                basicstyle=\ttfamily,
                keywordstyle=\color{NavyBlue}\ttfamily,
                stringstyle=\color{BurntOrange}\ttfamily,
                commentstyle=\color{PineGreen}\ttfamily,
                morecomment=[l][\color{RedViolet}]{\#},
                columns=flexible, %cancel monospced characters
                showstringspaces=false %not show space
}

%change the width of lrbox( lrbox is the environment form of \sbox, which is similar to \mbox)
\usepackage{varwidth}

%change baseline of pictures(eg: valign)
\usepackage[export]{adjustbox}

%declare \graybox to be a bin for saving box
\newsavebox{\graybox}

%note environment
\newenvironment{note}
    { \par\addvspace{\baselineskip}
      \centering
      \begin{minipage}{\textwidth} 
      \begin{lrbox}{\graybox} 
      \begin{varwidth}{0.9\textwidth}
    }
    { \end{varwidth}
      \end{lrbox}
      \includegraphics[width=0.077\textwidth,valign=m]{note.png}\hfill
      \colorbox{Gray!50}{\usebox{\graybox}}
      \end{minipage}
      \par\addvspace{\baselineskip}
     }

%tips enviroment       
\newenvironment{tips}
    { \par\addvspace{\baselineskip}
      \centering
      \begin{minipage}{\textwidth} 
      \begin{lrbox}{\graybox} 
      \begin{varwidth}{0.9\textwidth}
     }
     {\end{varwidth}
       \end{lrbox}
       \includegraphics[width=0.077\textwidth,valign=m]{tips.png}\hfill
       \colorbox{Gray!50}{\usebox{\graybox}}
       \end{minipage}
       \par\addvspace{\baselineskip}
     }

%warning enviroment    
\newenvironment{warning}
    { \par\addvspace{\baselineskip}
      \centering
      \begin{minipage}{\textwidth} 
      \begin{lrbox}{\graybox} 
      \begin{varwidth}{0.9\textwidth}
     }
     {\end{varwidth}
       \end{lrbox}
       \includegraphics[width=0.077\textwidth,valign=m]{warning.png}\hfill
       \colorbox{Gray!50}{\usebox{\graybox}}
       \end{minipage}
       \par\addvspace{\baselineskip}
     }

%complement enviroment
\newlength{\currentparindent}
\newenvironment{complement}[1]
      { \par\addvspace{\baselineskip}
        \setlength{\currentparindent}{\parindent}
        \centering
        \begin{lrbox}{\graybox}
        \begin{minipage}{\textwidth}
        \textbf{#1}
        \setlength{\parindent}{\currentparindent}
        {\color{Black}   \vspace{0.5ex} \hrule height 0.1ex} \vspace{0.5ex}}
      {
        \end{minipage}
        \end{lrbox}
        \colorbox{Gray!50}{\usebox{\graybox}}
        \par\addvspace{\baselineskip}
       }  

%type­sets text in mul­ti­ple columns (up to a max­i­mum of 10)
\usepackage{multicol}
\setlength\columnsep{15pt}

%revise\@endpart macro
\makeatletter
\def\@endpart{\bigbreak} 
\makeatother

%use \cmidrule \toprule
\usepackage{booktabs}


% add mini-ta­bles-of-con­tents (mini­tocs) at the be­gin­ning of ev­ery chap­ter, part or sec­tion.
\usepackage{minitoc}
\setcounter{minitocdepth}{1} 
\renewcommand\tightmtcfalse

%set two text form
\definecolor{Kblue}{rgb}{0.24,0.36,0.65}
\newcommand\known[1]{\textcolor{Blue}{\emph{#1}}}
\newcommand\vital[1]{\textcolor{Cyan}{\textbf{#1}}}

%exercise
\usepackage{xsim}
\usepackage[skins]{tcolorbox}
\xsimsetup{solution/print=false}
\DeclareExerciseEnvironmentTemplate{tcolorbox}{%
    \tcolorbox[enhanced,left=2pt,right=2pt,title=\large{\textbf{\GetExerciseName~\thesubsection}},% append .\GetExerciseProperty{counter} to count inside subsection,
        colback=white,colframe=black,colbacktitle=gray!50,coltitle=black,sharp corners=all]%
}{\endtcolorbox}
\DeclareExerciseType{myset}{
    exercise-env=myset,
    solution-env=mysetsol,
    exercise-name=Exercise Section,
    solution-name=Solution,
    exercise-template=tcolorbox,
    solution-template=tcolorbox,
    within=chapter,
    the-counter={\arabic{myset}},
}

\newcounter{myquestion}[chapter]
\newenvironment{myquestion}
  {\refstepcounter{myquestion}\textbf{Exercise \thechapter.\themyquestion :}}{}

\begin{document}
%initialize minitoc
\dominitoc 


\frontmatter
\maketitle
\chapter*{Preface}
\tableofcontents

\mainmatter

\chapter{Getting Started}
\minitoc 
    \section{Writing a Simple C++ Program}
        \subsection{Compiling and Executing Our Program}
    \begin{myset}
    \begin{myquestion}
Explain whether the following program fragment is legal.
    \begin{lstlisting}
std::cout << "The sum of " << v1;
<< " and " << v2;
<< " is " << v1 + v2 << std::endl;
    \end{lstlisting}    
If the program is legal, what does it do? If the program is not legal, why
not? How would you fix it?
    \end{myquestion}
    \end{myset}
    \section{A First Look at Input/Output}
    \section{A Word about Comments}
    \section{Flow of Control}
        \subsection{The while Statement}
        \subsection{The for Statement}
        \subsection{Reading an Unknown Number of Inputs}
        \subsection{The if Statement}
    \section{Introducing Classes}   
        \subsection{The Sales\_item Class}
        \subsection{A First Look at Member Functions}
    \section{The Bookstore Program}
    \section*{Chapter Summary} \markright{Chapter Summary} \addcontentsline{toc}{section}{Chapter Summary}
    \section*{Defined Terms} \markright{Defined Terms} \addcontentsline{toc}{section}{Defined Terms}

\part{The Basics}
\chapter{Variables and Basic Types}
\minitoc 
    \section{Primitive Built-in Types}
        \subsection{Arithmetic Types}
        \subsection{Type Conversions}
        \subsection{Literals}

\appendix
\chapter{The Library}
    \section{Library Names and Headers}


\backmatter

\chapter{Index}
\chapter{New features in C++11}

\end{document}

在此处输入图片描述

答案1

TeXnician 正确地识别了问题,注意到\end{lstlisting}被标记了,并建议将其删除。这里有几个教训。

  1. 在逐字环境中,使用空格和/或制表符的缩进会被环境捕获为数据,这与典型的 TeX 不同,在 TeX 中缩进的空格会被忽略。

  2. ^^I根据 TeX 惯例,逐字制表符显示为。为什么会这样?我们中那些年纪足够大、只记得处理 ASCII 的人会记得制表符 (ASCII 9) 相当于 CTL-I,其中I是字母表中的第 9 个字母(^历史上曾用于表示 CTL 键;因此我们这一代人立即将其识别^I为键盘输入 CTL-I)。

显然,Knuth 想要为那些已经熟悉当时广泛使用的语法的人保留这一概念。至于他如何在 TeX 中实现它,他在 TeXbook 第 45 页描述了一种巧妙的技术,即使用^^允许直接访问所有 128 个 ASCII 代码。

在此处输入图片描述

相关内容