我如何才能最好地创建类似于Table of Contents
、List of Figures
或 的自定义目录List of Tables
。为了举例说明,我将其称为“示例列表”。
假设我有一个自定义环境,它利用xparse
环境定义的便利性和可读性以及tcolorbox
多页支持的美观性/稳健性。一个已知问题是我错误地实现了可选参数,因为它不支持自动换行。此外,当没有提供可选参数时,Example 后面的冒号可能会导致示例列表看起来很滑稽。
\NewDocumentEnvironment{qikexample}{ O{} } % Optional Title, appears in List of Examples
{
\colorlet{colexam}{gray}
\newtcolorbox[use counter=qikexample,]{qikexamplebox}{%
% Example Frame Start
empty,% Empty previously set parameters
title={Example: #1},% use \thetcbcounter to access the qikexample counter text
% Attaching a box requires an overlay
attach boxed title to top left,
% (boxed title style requires an overlay)
boxed title style={empty,size=minimal,toprule=0pt,top=4pt,overlay={}},
coltitle=colexam,fonttitle=\bfseries,
before=\par\medskip\noindent,parbox=false,boxsep=0pt,left=0pt,right=3mm,top=2pt,breakable,pad at break=0mm,
before upper=\csname @totalleftmargin\endcsname0pt, % Use instead of parbox=true. This ensures parskip is inherited by box.
% Handles box when it exists on one page only
overlay unbroken={\draw[colexam,line width=.5pt] ([xshift=-10pt]title.north west) -- ([xshift=-10pt]frame.south west); },
% Handles multipage box: first page
overlay first={\draw[colexam,line width=.5pt] ([xshift=-10pt]title.north west) -- ([xshift=-10pt]frame.south west); },
% Handles multipage box: middle page
overlay middle={\draw[colexam,line width=.5pt] ([xshift=-10pt]frame.north west) -- ([xshift=-10pt]frame.south west); },
% Handles multipage box: last page
overlay last={\draw[colexam,line width=.5pt] ([xshift=-10pt]frame.north west) -- ([xshift=-10pt]frame.south west); },%
}
\begin{qikexamplebox}}
{\end{qikexamplebox}\endlist}
我将在这样的文档中实现这一点:
\begin{qikexample}[List Contents of Directory in Unix]
ls
\end{qikexample}
我想添加列出他们的姓名和页码的逻辑。
运行类似命令后\listofexamples
,输出可能如下所示:
示例列表
示例:列出 Unix 中的目录内容 ...................................... 23
示例:列出 Windows 中的目录内容 ................................ 23
代码
\documentclass{article}
\usepackage{fontspec}
\usepackage[most]{tcolorbox}
\usepackage{xparse}
% Counters
\newcounter{qikexample}
\NewDocumentEnvironment{qikexample}{ O{} } % Optional Title, appears in List of Examples
{
\colorlet{colexam}{gray}
\newtcolorbox[use counter=qikexample,]{qikexamplebox}{%
% Example Frame Start
empty,% Empty previously set parameters
title={Example: #1},% use \thetcbcounter to access the qikexample counter text
% Attaching a box requires an overlay
attach boxed title to top left,
% (boxed title style requires an overlay)
boxed title style={empty,size=minimal,toprule=0pt,top=4pt,overlay={}},
coltitle=colexam,fonttitle=\bfseries,
before=\par\medskip\noindent,parbox=false,boxsep=0pt,left=0pt,right=3mm,top=2pt,breakable,pad at break=0mm,
before upper=\csname @totalleftmargin\endcsname0pt, % Use instead of parbox=true. This ensures parskip is inherited by box.
% Handles box when it exists on one page only
overlay unbroken={\draw[colexam,line width=.5pt] ([xshift=-10pt]title.north west) -- ([xshift=-10pt]frame.south west); },
% Handles multipage box: first page
overlay first={\draw[colexam,line width=.5pt] ([xshift=-10pt]title.north west) -- ([xshift=-10pt]frame.south west); },
% Handles multipage box: middle page
overlay middle={\draw[colexam,line width=.5pt] ([xshift=-10pt]frame.north west) -- ([xshift=-10pt]frame.south west); },
% Handles multipage box: last page
overlay last={\draw[colexam,line width=.5pt] ([xshift=-10pt]frame.north west) -- ([xshift=-10pt]frame.south west); },%
}
\begin{qikexamplebox}}
{\end{qikexamplebox}\endlist}
\begin{document}
%\listofexamples
\begin{qikexample}[List Contents of Directory in Unix]
ls
\end{qikexample}
\end{document}
答案1
所有这些都已包含在tcolorbox
选项list inside=
和\tcblistof
命令中。
首先定义\newtcolorbox[other options,list inside=qex]{...}
生成一个带有扩展.toc
名的新文件.qex
(快速示例),然后使用\tcblistof[\section*]{qex}{Some title}
。
\documentclass{article}
\usepackage{fontspec}
\usepackage[most]{tcolorbox}
\usepackage{xparse}
\newcounter{qikexample}
\NewDocumentEnvironment{qikexample}{ O{} } % Optional Title, appears in List of Examples
{
\colorlet{colexam}{gray}
\newtcolorbox[use counter=qikexample,list inside=qex]{qikexamplebox}{%
% Example Frame Start
empty,% Empty previously set parameters
title={Example: #1},% use \thetcbcounter to access the qikexample counter text
% Attaching a box requires an overlay
attach boxed title to top left,
% (boxed title style requires an overlay)
boxed title style={empty,size=minimal,toprule=0pt,top=4pt,overlay={}},
coltitle=colexam,fonttitle=\bfseries,
before=\par\medskip\noindent,parbox=false,boxsep=0pt,left=0pt,right=3mm,top=2pt,breakable,pad at break=0mm,
before upper=\csname @totalleftmargin\endcsname0pt, % Use instead of parbox=true. This ensures parskip is inherited by box.
% Handles box when it exists on one page only
overlay unbroken={\draw[colexam,line width=.5pt] ([xshift=-10pt]title.north west) -- ([xshift=-10pt]frame.south west); },
% Handles multipage box: first page
overlay first={\draw[colexam,line width=.5pt] ([xshift=-10pt]title.north west) -- ([xshift=-10pt]frame.south west); },
% Handles multipage box: middle page
overlay middle={\draw[colexam,line width=.5pt] ([xshift=-10pt]frame.north west) -- ([xshift=-10pt]frame.south west); },
% Handles multipage box: last page
overlay last={\draw[colexam,line width=.5pt] ([xshift=-10pt]frame.north west) -- ([xshift=-10pt]frame.south west); },%
}
\begin{qikexamplebox}}
{\end{qikexamplebox}\endlist}
\begin{document}
%\listofexamples
\tcblistof[\section*]{qex}{List of Examples}
\begin{qikexample}[List Contents of Directory in Unix]
ls
\end{qikexample}
\begin{qikexample}[Delete all]
rm *.*
\end{qikexample}
\end{document}
这是 Harish Kumar(不幸的是他不再是 TeX.SE 的用户了)偷偷塞进我的答案中的代码 ;-)
tcolorbox
xparse
提供与加载库时类似的功能的选项\tcbuselibrary{xparse}
。例如,可以通过以下方式生成您的框:
\colorlet{colexam}{gray}
\DeclareTColorBox[use counter=qikexample,list inside=qex]{qikexample}{ O{} }{%
% Example Frame Start
empty,% Empty previously set parameters
title={Example: #1},% use \thetcbcounter to access the qikexample counter text
% Attaching a box requires an overlay
attach boxed title to top left,
% (boxed title style requires an overlay)
boxed title style={empty,size=minimal,toprule=0pt,top=4pt,overlay={}},
coltitle=colexam,fonttitle=\bfseries,
before=\par\medskip\noindent,parbox=false,boxsep=0pt,left=0pt,right=3mm,top=2pt,breakable,pad at break=0mm,
before upper=\csname @totalleftmargin\endcsname0pt, % Use instead of parbox=true. This ensures parskip is inherited by box.
% Handles box when it exists on one page only
overlay unbroken={\draw[colexam,line width=.5pt] ([xshift=-10pt]title.north west) -- ([xshift=-10pt]frame.south west); },
% Handles multipage box: first page
overlay first={\draw[colexam,line width=.5pt] ([xshift=-10pt]title.north west) -- ([xshift=-10pt]frame.south west); },
% Handles multipage box: middle page
overlay middle={\draw[colexam,line width=.5pt] ([xshift=-10pt]frame.north west) -- ([xshift=-10pt]frame.south west); },
% Handles multipage box: last page
overlay last={\draw[colexam,line width=.5pt] ([xshift=-10pt]frame.north west) -- ([xshift=-10pt]frame.south west); },%
}
\NewDocumentEnvironment
我很疑惑是否真的需要外层!
新版本具有以下特征的tocloft
组合tcolorbox
:
该tocloft
命令\newlistof
将自动定义计数器和\l@....
命令以及\listof...
宏。
由于tcolorbox
使用计数器qikexample
,因此有必要说
\newcommand{\listofexamplesname}{List of Examples}
\newlistof{qikexample}{qex}{\listofexamplesname}
\renewcommand{\cftqikexampledotsep}{\cftnodots}
\NewDocumentCommand{\listofexamples}{}{%
\listofqikexample
}
现在\listofexamples
是一个\listofqikexample
使用\l@qikexample
条目类型的包装器。
这必须在选项list type=qikexample
中指定tcolorbox
。
现在所有\cftX....
宏都可用,请参见示例\cftqikexampledotsep
。
这是修改后的示例
\documentclass{article}
\usepackage{fontspec}
\usepackage[most]{tcolorbox}
\usepackage{xparse}
\usepackage{tocloft}
%\newcounter{qikexample}
\newcommand{\listofexamplesname}{List of Examples}
\newlistof{qikexample}{qex}{\listofexamplesname}
\renewcommand{\cftqikexampledotsep}{\cftnodots}
\NewDocumentCommand{\listofexamples}{}{%
\listofqikexample
}
\NewDocumentEnvironment{qikexample}{ O{} } % Optional Title, appears in List of Examples
{
\colorlet{colexam}{gray}
\newtcolorbox[use counter=qikexample,list inside=qex,list type=qikexample]{qikexamplebox}{%
% Example Frame Start
empty,% Empty previously set parameters
title={Example: #1},% use \thetcbcounter to access the qikexample counter text
% Attaching a box requires an overlay
attach boxed title to top left,
% (boxed title style requires an overlay)
boxed title style={empty,size=minimal,toprule=0pt,top=4pt,overlay={}},
coltitle=colexam,fonttitle=\bfseries,
before=\par\medskip\noindent,parbox=false,boxsep=0pt,left=0pt,right=3mm,top=2pt,breakable,pad at break=0mm,
before upper=\csname @totalleftmargin\endcsname0pt, % Use instead of parbox=true. This ensures parskip is inherited by box.
% Handles box when it exists on one page only
overlay unbroken={\draw[colexam,line width=.5pt] ([xshift=-10pt]title.north west) -- ([xshift=-10pt]frame.south west); },
% Handles multipage box: first page
overlay first={\draw[colexam,line width=.5pt] ([xshift=-10pt]title.north west) -- ([xshift=-10pt]frame.south west); },
% Handles multipage box: middle page
overlay middle={\draw[colexam,line width=.5pt] ([xshift=-10pt]frame.north west) -- ([xshift=-10pt]frame.south west); },
% Handles multipage box: last page
overlay last={\draw[colexam,line width=.5pt] ([xshift=-10pt]frame.north west) -- ([xshift=-10pt]frame.south west); },%
}
\begin{qikexamplebox}}
{\end{qikexamplebox}\endlist}
\begin{document}
\listofexamples
\begin{qikexample}[List Contents of Directory in Unix]
ls
\end{qikexample}
\begin{qikexample}[Delete all]
rm *.*
\end{qikexample}
\end{document}
答案2
我建议您使用tocloft
在序言中创建以下内容的包:
\newcommand{\listexamplesname}{List of examples}
\newlistof{qikexample}{xmp}{\listexamplesname}
然后,您可以在qikexample
环境中使用\refstepcounter{qikexample}
来推进计数器和
\addcontentsline{xmp}{example}
{\protect\numberline{\thesection.\theqikexample}#1}\par
将其添加到列表中。然后在文档中\listofqikexamples
打印它。
编辑:我受到启发这帖子,并尝试在此总结其内容。
这是一个可编译的示例:
\documentclass{article}
\usepackage{fontspec}
\usepackage{tocloft}
\usepackage{etoolbox}
\usepackage[most]{tcolorbox}
\usepackage{xparse}
% Counters
\newcommand{\listexamplesname}{List of examples}
\newlistof{qikexample}{xmp}{\listexamplesname}
\NewDocumentEnvironment{qikexample}{ O{} } % Optional Title, appears in List of Examples
{
\refstepcounter{qikexample}
\addcontentsline{xmp}{example}
{\protect\numberline{\thesection.\theqikexample}#1}\par
\colorlet{colexam}{gray}
\newtcolorbox{qikexamplebox}{%
% Example Frame Start
empty,% Empty previously set parameters
title={Example: #1},% use \thetcbcounter to access the qikexample counter text
% Attaching a box requires an overlay
attach boxed title to top left,
% (boxed title style requires an overlay)
boxed title style={empty,size=minimal,toprule=0pt,top=4pt,overlay={}},
coltitle=colexam,fonttitle=\bfseries,
before=\par\medskip\noindent,parbox=false,boxsep=0pt,left=0pt,right=3mm,top=2pt,breakable,pad at break=0mm,
before upper=\csname @totalleftmargin\endcsname0pt, % Use instead of parbox=true. This ensures parskip is inherited by box.
% Handles box when it exists on one page only
overlay unbroken={\draw[colexam,line width=.5pt] ([xshift=-10pt]title.north west) -- ([xshift=-10pt]frame.south west); },
% Handles multipage box: first page
overlay first={\draw[colexam,line width=.5pt] ([xshift=-10pt]title.north west) -- ([xshift=-10pt]frame.south west); },
% Handles multipage box: middle page
overlay middle={\draw[colexam,line width=.5pt] ([xshift=-10pt]frame.north west) -- ([xshift=-10pt]frame.south west); },
% Handles multipage box: last page
overlay last={\draw[colexam,line width=.5pt] ([xshift=-10pt]frame.north west) -- ([xshift=-10pt]frame.south west); },%
}
\begin{qikexamplebox}}
{\end{qikexamplebox}\endlist}
\begin{document}
\listofqikexample
\begin{qikexample}[List Contents of Directory in Unix]
ls
\end{qikexample}
\end{document}