提问于:
浏览数:
2242
![](https://wenda.latexstudio.net/data/attach/200808/WjpoMrIl.png)
请教各位大佬,如何实现图片中的目录样式?
2 回答
2
提问没有给具体信息,所以只在标准文档类 `book` 下做了实现,不保证任何兼容性。实现主要由两部分构成
- 在 `.toc` 文件中,把 `chapter` 以下的条目包在某个环境中。把 `chapter` 的标题保存下来,作为参数传给环境。
- 这个环境用 `tcolorbox` 实现,方便控制样式。
知乎文章《[[LaTeX 尝试] 一种目录样式:章名居左、多个节名居右](https://zhuanlan.zhihu.com/p/176992118)》提供了更详细的介绍和解释。
```tex
\documentclass[openany]{book}
\usepackage{xpatch}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usepackage{tcolorbox}
\tcbuselibrary{skins, hooks} % actually skins lib loads tikz pkg
%TODO
% - breakable
\makeatletter
%\tcbset{
% % similar to def of option `attach boxed title to left top`
% attach boxed title to left/.style={
% % used in \tcbset{#1}\node[#3, at={(frame.#2)}] {\tcbox{<title>}};
% attach@title@boxed@top={#1}{left}{west}
% },
%}
\newtcolorbox{tocchap}[1]{%
enhanced,
% box
beforeafter skip=0pt,
width=.6\linewidth,
flush right,
frame empty,
interior empty,
overlay={
% left line
\draw[line width=2pt, cyan] ([yshift=-15pt]frame.north west) -- ([yshift=15pt]frame.south west);
% title
% hekip between title and left line is caused by tikz option `node distance`
\node[left=of frame, draw] {\tcbtitle};
},
% title
detach title,
coltitle=cyan,
fonttitle=\large,
% attach boxed title to left,
% boxed title style={
% frame empty,
% interior empty
% },
% allow manual linebreak in title text
title={\parbox{100pt}{\centering #1}},
}
\newif\if@first@chapter
\@first@chaptertrue
% wrap toc entries lower than chapter in `tocchap` env
% expected contents of a .toc file:
% \contentsline {chapter}{\numberline {1}title}{2}%
% \begin {tocchap}{\@currentchapter }%
% \contentsline {section}{\numberline {1.1}title}{2}%
% \contentsline {section}{\numberline {1.2}title}{2}%
% \end {tocchap}%
%% default definition of \addcontentsline:
%% #1 = one of aux extension, e.g., "toc", "lof", ...
%% #2 = toc entry name, e.g., chapter, section, table, ...
%% #3 = toc entry title, e.g., \numberline{...}...
% \addtocontents{#1}{\protect\contentsline{#2}{#3}{\thepage}%
% \protected@file@percent}}
\xpretocmd\addcontentsline{%
\expandafter\ifx\csname the#2\endcsname\thechapter
\if@first@chapter
\global\@first@chapterfalse
\else
\addtocontents{#1}{\protect\end{tocchap}\protected@file@percent}%
\fi
\fi
}{}{\fail}
\xapptocmd\addcontentsline{%
\expandafter\ifx\csname the#2\endcsname\thechapter
\addtocontents{#1}{\protect\begin{tocchap}{\protect\@currentchapter}\protected@file@percent}
\fi%
}{}{\fail}
\AtEndDocument{%
\addtocontents{toc}{\protect\end{tocchap}\protected@file@percent}%
}
% store chapter title in \@currentchapter
\renewcommand*\l@chapter[2]{\def\@currentchapter{\let\numberline\@gobble#1}}
% reduce left skip of \l@section
\renewcommand*\l@section{\@dottedtocline{1}{0em}{2.3em}}
\makeatother
\begin{document}
\tableofcontents
\chapter{Title}
\section{title}
\section{title}
\chapter[Long long long \protect\\long title]{Long long long long title}
\section{title}
\section{title}
\section{title}
\section{title}
\end{document}
```
![](https://wenda.latexstudio.net/data/attach/200809/dlcyvYU5.png)
0
私以为这个样式值 50 元以上,你想白嫖就别想了
作者追问:2020-08-08 22:39
只要能实现,100,150也行。但请你不要用白嫖这样的词
你的回答
请登录后回答
你的回答将会帮助更多人,请务必认真回答问题。