使用 tcolorbox 在目录中同时进行左右对齐

使用 tcolorbox 在目录中同时进行左右对齐

tcolorbox 可以有标题和后标题。是否可以将它们同时放在目录中,标题左对齐,后标题右对齐?

  \documentclass[11pt]{book}


  \RequirePackage[skins,listings,breakable,listingsutf8,theorems,hooks,fitting]{tcolorbox}% 

  \begin{document}
  \tableofcontents
  \vglue 3cm

  \def\BoxTitle{tcolorbox Title}
  \def\BoxAfterTitle{tcolorbox After Title}

  % taken from http://tex.stackexchange.com/questions/86711/tcolorbox-list-of-listings
  \makeatletter
  \tcbset{%
  addtotoc/.style={list entry={\BoxTitle\dotfill\BoxAfterTitle},add to list={toc}{section}},
  }
  \makeatother


  \begin{tcolorbox}%
  [after title=\hfill\BoxAfterTitle,%
  title=\BoxTitle,%
  addtotoc
  ]%%
  How to achieve that ?
  \begin{itemize}
  \item Title = left justified in the TOC.
  \item After Title = right justified in the TOC
  \item Same kind of dots in between . . . . . .  whether or not there are dots after the After Title in the TOC
  \end{itemize}
  \end{tcolorbox}


  \def\BoxTitle{Long Long Long Second Title}
  \def\BoxAfterTitle{Short Second After Title}

   \begin{tcolorbox}%
  [after title=\hfill \BoxAfterTitle,%
  title=\BoxTitle,%
  addtotoc
  ]%%
  Second tcolorbox
   \end{tcolorbox}

  \def\BoxTitle{Third Title}
  \def\BoxAfterTitle{Long Long Long Third After Title}

   \begin{tcolorbox}%
  [after title=\hfill \BoxAfterTitle,%
  title=\BoxTitle,%
  addtotoc
  ]%%
  Third tcolorbox
   \end{tcolorbox}


  \end{document}  

在此处输入图片描述

答案1

我认为,您必须直接调整目录条目格式。也许有一些不错的格式化包可以做到这一点,但您可以使用以下手工解决方案:

\documentclass[11pt]{book}


\RequirePackage[skins,listings,breakable,listingsutf8,theorems,hooks,fitting]{tcolorbox}%

\makeatletter

\newcommand*\l@myentry{\@mydottedtocline{1}{1.5em}{2.3em}}

\def\@mydottedtocline#1#2#3#4#5{%
  \ifnum #1>\c@tocdepth \else
    \vskip \z@ \@plus.2\p@
    {\leftskip #2\relax \rightskip \@tocrmarg \parfillskip -\rightskip
     \parindent #2\relax\@afterindenttrue
     \interlinepenalty\@M
     \leavevmode
     \@tempdima #3\relax
     \advance\leftskip \@tempdima \null\nobreak\hskip -\leftskip
     {#4}\nobreak
     \hb@xt@\@pnumwidth{\hfil\normalfont \normalcolor #5}%
     \par}%
  \fi}

\def\myfill{%
     \leaders\hbox{$\m@th
        \mkern \@dotsep mu\hbox{.}\mkern \@dotsep
        mu$}\hfill
     \nobreak
}

\tcbset{%
addtotoc/.style={list entry={\BoxTitle\protect\myfill\BoxAfterTitle},add to list={toc}{myentry}},
}
\makeatother



\begin{document}
\tableofcontents
\vglue 3cm

\def\BoxTitle{tcolorbox Title}
\def\BoxAfterTitle{tcolorbox After Title}


\begin{tcolorbox}%
[after title=\hfill\BoxAfterTitle,%
title=\BoxTitle,%
addtotoc
]%%
How to achieve that ?
\begin{itemize}
\item Title = left justified in the TOC.
\item After Title = right justified in the TOC
\item Same kind of dots in between . . . . . .  whether or not there are dots after the After Title in the TOC
\end{itemize}
\end{tcolorbox}


\def\BoxTitle{Long Long Long Second Title}
\def\BoxAfterTitle{Short Second After Title}

 \begin{tcolorbox}%
[after title=\hfill \BoxAfterTitle,%
title=\BoxTitle,%
addtotoc
]%%
Second tcolorbox
 \end{tcolorbox}

\def\BoxTitle{Third Title}
\def\BoxAfterTitle{Long Long Long Third After Title}

 \begin{tcolorbox}%
[after title=\hfill \BoxAfterTitle,%
title=\BoxTitle,%
addtotoc
]%%
Third tcolorbox
 \end{tcolorbox}


\end{document}

在此处输入图片描述

如果您不需要\BoxTitle\BoxAfterTitle宏,我可以提供一个具有相同输出的更紧凑的解决方案:

\documentclass[11pt]{book}

\RequirePackage[skins,listings,breakable,listingsutf8,theorems,hooks,fitting]{tcolorbox}%

\makeatletter

\newcommand*\l@myentry{\@mydottedtocline{1}{1.5em}{2.3em}}

\def\@mydottedtocline#1#2#3#4#5{%
  \ifnum #1>\c@tocdepth \else
    \vskip \z@ \@plus.2\p@
    {\leftskip #2\relax \rightskip \@tocrmarg \parfillskip -\rightskip
     \parindent #2\relax\@afterindenttrue
     \interlinepenalty\@M
     \leavevmode
     \@tempdima #3\relax
     \advance\leftskip \@tempdima \null\nobreak\hskip -\leftskip
     {#4}\nobreak
     \hb@xt@\@pnumwidth{\hfil\normalfont \normalcolor #5}%
     \par}%
  \fi}

\def\myfill{%
     \leaders\hbox{$\m@th
        \mkern \@dotsep mu\hbox{.}\mkern \@dotsep
        mu$}\hfill
     \nobreak
}

\tcbset{%
mytitle/.style 2 args={%
  title={#1\hfill#2},%
  list entry={#1\protect\myfill#2},%
  add to list={toc}{myentry}%
}}
\makeatother



\begin{document}
\tableofcontents
\vglue 3cm

\begin{tcolorbox}[mytitle={tcolorbox Title}{tcolorbox After Title}]%%
How to achieve that ?
\begin{itemize}
\item Title = left justified in the TOC.
\item After Title = right justified in the TOC
\item Same kind of dots in between . . . . . .  whether or not there are dots after the After Title in the TOC
\end{itemize}
\end{tcolorbox}

\begin{tcolorbox}[mytitle={Long Long Long Second Title}{Short Second After Title}]%%
Second tcolorbox
\end{tcolorbox}

\begin{tcolorbox}[mytitle={Third Title}{Long Long Long Third After Title}]%%
Third tcolorbox
\end{tcolorbox}

\end{document}

相关内容