我想在目录中的标题周围添加彩色框。该框应从页面边缘开始,并在章节名称结束后结束。
我找到了很多解决方案,可以创建一个从章节号到页码的框。但是,我找不到一个符合我需求的。
注意:由于字体集成问题,我正在使用 XeLatex。
编辑
如前所述,我需要使用 XeLatex 来支持 unicode。因此,Tikz 不是一个好的解决方案,因为 tikzmark 和 pgfmark 得不到很好的支持(根据为什么 xelatex 在 tikz 图形中呈现的箭头与 lualatex 不同?以及我的无数次尝试)。
我设法使某些部分正常工作:
\documentclass[a4paper,11pt, american]{report}
\usepackage[utf8]{inputenc}
\usepackage{tocloft,tikz}
\usetikzlibrary{calc}
\makeatletter
\newcommand{\gettikzxy}[3]{%
\tikz@scan@one@point\pgfutil@firstofone#1\relax
\edef#2{\the\pgf@x}%
\edef#3{\the\pgf@y}%
}
\makeatother
\newcommand*{\tikzcoordinate}[1]{%
\tikz[remember picture, overlay] \coordinate (#1);}
\renewcommand{\cftchapfont}{%
\tikzcoordinate{A}
\bfseries\Large%
}
\renewcommand{\cftchappagefont}{\bfseries}
\renewcommand{\cftchapleader}{%
\tikz[remember picture,overlay]{%
\coordinate (C);%
\coordinate (B) at (current page.west|- A);
\gettikzxy{(A)}{\ax}{\ay}
\gettikzxy{(B)}{\bx}{\by}
\gettikzxy{(C)}{\cx}{\cy}
\fill[red,opacity=.2] ([yshift=+1.3333em,xshift=-.3333em]B) rectangle
([xshift=.3333em,yshift=+-1ex]C |- 0,0);
\ifx\by\cy
\else
\coordinate (D) at (\cx+.3333em,\by+1.3333em);
\coordinate (E) at (\ax-.8em+\linewidth- \cftchapindent- \cftchapnumwidth,\cy+1em);
\gettikzxy{(D)}{\dx}{\dy}
\gettikzxy{(E)}{\ex}{\ey}
\ifdim\dx<\ex
\fill[red,opacity=.2] (D) rectangle (E);
\else
\fill[white,opacity=1] (D) rectangle (E);
\fi
\fi
}%
\bfseries\cftdotfill{\cftdotsep}%
}
\begin{document}
\tableofcontents
\chapter{(title chapter 1)}
\section{(title section 1)}
\subsection{(title sub-section 1)}
\subsection{(title sub-section 2)}
\section{(title section 2)}
\subsection{(title sub-section 1)}
\subsection{(title sub-section 2)}
\chapter{(veryvery very very very very very very very very very very very very very very very very very very very very very long title chapter 2)}
\section{(title section 1)}
\subsection{(title sub-section 1)}
\subsection{(title sub-section 2)}
\section{(title section 2)}
\subsection{(title sub-section 1)}
\subsection{(title sub-section 2)}
\chapter{(very very very very very very very very long title chapter 3)}
\section{(title section 1)}
\subsection{(title sub-section 1)}
\subsection{(title sub-section 2)}
\section{(title section 2)}
\subsection{(title sub-section 1)}
\subsection{(title sub-section 2)}
\end{document}
现在,我的问题是我需要在背景上使用颜色,以便文本可以以白色显示在背景上。有关于如何使用 XeLatex 实现这一点的提示吗?
答案1
终于找到了解决方案。使用 tikzmark 的问题不是 XeLatex。但是,必须使用计数器来确保每个标记名称都是唯一的。可能有一个更简单的解决方案,但这个解决方案运行良好。
\documentclass{report}
\usepackage{tocloft,tikz}
\usetikzlibrary{calc}
\usetikzlibrary{tikzmark}
\makeatletter
\newcommand{\gettikzxy}[3]{%
\tikz@scan@one@point\pgfutil@firstofone#1\relax
\edef#2{\the\pgf@x}%
\edef#3{\the\pgf@y}%
}
\makeatother
\newcounter{chapmarkcounter}
\setcounter{chapmarkcounter}{1}
\renewcommand{\cftchapfont}{%
\def\markname{cs:cmark:\expandafter\thechapmarkcounter}%
\tikz[remember picture,overlay]{%
\coordinate (A);%
\coordinate (B) at (current page.west|- A);%
\coordinate (C) at (pic \expandafter\markname);%
\gettikzxy{(A)}{\ax}{\ay}%
\gettikzxy{(B)}{\bx}{\by}%
\gettikzxy{(C)}{\cx}{\cy}%
\fill[red] (\bx-.3333em, \by+1.3333em) rectangle (\cx + .3333em, \cy-0.9ex);%
\ifx\by\cy%
\else%
\coordinate (D) at (\cx+.3333em,\by+1.3333em);%
\coordinate (E) at (\ax-.8em+\linewidth- \cftchapindent- \cftchapnumwidth,\cy+1em);%
\gettikzxy{(D)}{\dx}{\dy}%
\gettikzxy{(E)}{\ex}{\ey}%
\ifdim\dx<\ex%
\fill[red] (D) rectangle (E);%
\else%
\fill[white] (D) rectangle (E);%
\fi%
\fi%
}%
\color{white}\bfseries\Large%
}
\renewcommand{\cftchappagefont}{\Large\bfseries}
\renewcommand{\cftchapleader}{%
\pgfmark{cmark:\expandafter\thechapmarkcounter}%
\addtocounter{chapmarkcounter}{1}%
\bfseries\cftdotfill{\cftdotsep}%
}
\begin{document}
\tableofcontents
\chapter{(title chapter 1)}
\section{(title section 1)}
\subsection{(title sub-section 1)}
\subsection{(title sub-section 2)}
\section{(title section 2)}
\subsection{(title sub-section 1)}
\subsection{(title sub-section 2)}
\chapter{(veryvery very very very very very very very very very very very very very very very very very very very very very long title chapter 2)}
\section{(title section 1)}
\subsection{(title sub-section 1)}
\subsection{(title sub-section 2)}
\section{(title section 2)}
\subsection{(title sub-section 1)}
\subsection{(title sub-section 2)}
\chapter{(very very very very very very very very long title chapter 3)}
\section{(title section 1)}
\subsection{(title sub-section 1)}
\subsection{(title sub-section 2)}
\section{(title section 2)}
\subsection{(title sub-section 1)}
\subsection{(title sub-section 2)}
\chapter{(title chapter 4)}
\section{(title section 1)}
\subsection{(title sub-section 1)}
\subsection{(title sub-section 2)}
\section{(title section 2)}
\subsection{(title sub-section 1)}
\subsection{(title sub-section 2)}
\end{document}