1
\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}

enter image description here

6
  • The problem is a tab in front of \end{lstlisting}. Remove that and it won't appear.
    – TeXnician
    Commented Sep 20, 2017 at 15:08
  • To amplify TeXnician's comment, the symbol ^^I is the way TeX denotes an ASCII tab character. See TeXbook, p.45, 343-344, 351, 368, 370, & 391. Commented Sep 20, 2017 at 15:11
  • @TeXnician interesting, can you tell me the reason about it.
    – K.Robert
    Commented Sep 20, 2017 at 15:11
  • 1
    On TeXbook, page 45, "TEX has a standard way to refer to the invisible characters of ASCII: Code 0 can be typed as the sequence of three characters ^^@, code 1 can be typed ^^A, and so on up to code 31, which is ^^_ (see Appendix C). If the character following ^^ has an internal code between 64 and 127, TEX subtracts 64 from the code; if the code is between 0 and 63, TEX adds 64. Hence code 127 can be typed ^^?, and the dangerous bend sign can be obtained by saying {\manual^^?}..." Commented Sep 20, 2017 at 15:13
  • "...However, you must change the category code of character 127 before using it, since this character ordinarily has category 15 (invalid); say, e.g., \catcode‘\^^?=12. The ^^ notation is different from \char, because ^^ combinations are like single characters; for example, it would not be permissible to say \catcode‘\char127, but ^^ symbols can even be used as letters within control words." Commented Sep 20, 2017 at 15:13

1 Answer 1

2

TeXnician properly identified the problem by noting the \end{lstlisting} was tabbed in, and advised to remove it. There are several lessons here.

  1. In verbatim environments, indenting with spaces and/or tabs is captured as data by the environment, unlike typical TeX, where indented space is ignored.

  2. A verbatim tab is displayed as ^^I, according to TeX convention. Why is this? Those of us old enough to remember dealing exclusively in ASCII recall that a tab (ASCII 9) is equivalent to a CTL-I, where I is the 9th letter of the alphabet (and ^ was historically used to denote the CTL key; thus my generation immediately recognizes ^I as the keyboard entry CTL-I).

It seems obvious that Knuth wanted to preserve that notion for those already familiar with that widely used at the time syntax. As to how he implemented it in TeX, he describes the neat technique on page 45 of the TeXbook, of using ^^ to permit direct access to all 128 ASCII codes.

enter image description here

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .