\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}
%typesets text in multiple columns (up to a maximum of 10)
\usepackage{multicol}
\setlength\columnsep{15pt}
%revise\@endpart macro
\makeatletter
\def\@endpart{\bigbreak}
\makeatother
%use \cmidrule \toprule
\usepackage{booktabs}
% add mini-tables-of-contents (minitocs) at the beginning of every chapter, part or section.
\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}}}
\usepackage{nameref}
\begin{document}
%initialize minitoc
\dominitoc
\frontmatter
\maketitle
\chapter*{Preface}
\tableofcontents
\chapter*{Important Sections}
\ref{subsec:compiling and executing our rogram} \nameref{subsec:compiling and executing our rogram} \hfill \pageref{subsec:compiling and executing our rogram}\\
\ref{subsec:The while Statement} \nameref{subsec:The while Statement} \hfill \pageref{subsec:The while Statement}\\
\ref{subsec:A First Look at Member Functions} \nameref{subsec:A First Look at Member Functions} \hfill \pageref{subsec:A First Look at Member Functions}
\mainmatter
\chapter{Getting Started}
\minitoc
\section{Writing a Simple C++ Program}
\subsection{Compiling and Executing Our Program}\label{subsec:compiling and executing our rogram}
\section{A First Look at Input/Output}
\section{A Word about Comments}
\section{Flow of Control}
\subsection{The while Statement}\label{subsec:The while Statement}
\subsection{The for Statement}
\subsection{Reading an Unknown Number of Inputs}
\subsection{The if Statement}\label{subsec:The if Statement}
\section{Introducing Classes}
\subsection{The Sales\_item Class}
\subsection{A First Look at Member Functions}\label{subsec: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}
\ref
我想在“重要章节”一章中列出一些我认为重要的章节?我发现一个愚蠢的方法,就是在每个重要章节上使用\nameref
\pageref
。但我要列出的章节太多了,有没有更聪明的方法?
答案1
\implabel
这遵循了使用而不是\label
来指示重要部分的想法。
所有这些标签都存储在一个\seq
列表中expl3
,稍后.aux
以逗号分隔的列表形式写入文件,并\label{allimportantsections}
在文档末尾自动写入,在文档开始时,检索此标签并填充已读标签列表。
宏对环境\DisplayImportantSections
中的引用进行排版tabularx
。
\documentclass[openany]{book}
\usepackage{tabularx}
\usepackage{array}
\usepackage{refcount}
\usepackage{xparse}
\newcolumntype{Y}{>{\raggedleft}X}
\ExplSyntaxOn
\seq_new:N \g_robert_importantlabels_seq
\seq_new:N \g_robert_importantlabels_read_seq
\NewDocumentCommand{\addlabel}{m}{%
\seq_gput_right:Nn \g_robert_importantlabels_seq {#1}%
}
\NewDocumentCommand{\looplabels}{}{%
\seq_map_inline:Nn \g_robert_importantlabels_read_seq {
\DisplayImportantSecEntry{##1}
}
}
\cs_generate_variant:Nn \seq_gset_from_clist:Nn {Nx}
\makeatletter
\AtEndDocument{%
\protected@edef\@currentlabel{\seq_use:Nn \g_robert_importantlabels_seq {,}}
\label{allimportantsections}
}
\AtBeginDocument{%
\seq_gset_from_clist:Nx \g_robert_importantlabels_read_seq {\getrefnumber{allimportantsections}}
}
\makeatother
\ExplSyntaxOff
\NewDocumentCommand{\DisplayImportantSections}{}{%
\begin{tabularx}{\linewidth}{rXY}
\looplabels
\end{tabularx}
}
\NewDocumentCommand{\DisplayImportantSecEntry}{m}{%
\ref{#1} & \nameref{#1} & \pageref{#1} \tabularnewline
}
\NewDocumentCommand{\implabel}{m}{%
\label{#1}%
\addlabel{#1}%
}
\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}
%typesets text in multiple columns (up to a maximum of 10)
\usepackage{multicol}
\setlength\columnsep{15pt}
%revise\@endpart macro
\makeatletter
\def\@endpart{\bigbreak}
\makeatother
%use \cmidrule \toprule
\usepackage{booktabs}
% add mini-tables-of-contents (minitocs) at the beginning of every chapter, part or section.
\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}}}
\usepackage{nameref}
\begin{document}
%initialize minitoc
\dominitoc
\frontmatter
\maketitle
\chapter*{Preface}
\tableofcontents
\chapter*{Important Sections}
\DisplayImportantSections
%\ref{subsec:compiling and executing our rogram} \nameref{subsec:compiling and executing our rogram} \hfill \pageref{subsec:compiling and executing our rogram}\\
%\ref{subsec:The while Statement} \nameref{subsec:The while Statement} \hfill \pageref{subsec:The while Statement}\\
%\ref{subsec:A First Look at Member Functions} \nameref{subsec:A First Look at Member Functions} \hfill \pageref{subsec:A First Look at Member Functions}
\mainmatter
\chapter{Getting Started}
\minitoc
\section{Writing a Simple C++ Program}
\subsection{Compiling and Executing Our Program}\implabel{subsec:compiling and executing our rogram}
\section{A First Look at Input/Output}
\section{A Word about Comments}
\section{Flow of Control}
\subsection{The while Statement}\implabel{subsec:The while Statement}
\subsection{The for Statement}
\subsection{Reading an Unknown Number of Inputs}
\subsection{The if Statement}\label{subsec:The if Statement}
\section{Introducing Classes}
\subsection{The Sales\_item Class}
\subsection{A First Look at Member Functions}\implabel{subsec: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}