如何在章节编号(包括目录)后加上一个星号

发布于 2021-12-24 18:53:19

比如
4.1 foo
4.2* fooo
这个星号需要加在4.2的后面,同时在目录显示中也要加
我用titleformat在thesection后面加了一个,但是这样目录中没有,目录这部分不会,请教一下怎么做,另外thesection后面我放了一个tok然后每次把tok在和空之间来回改,不知道有没有更好的办法
谢谢
f76659d9cd27ea406b9e6113d049607c.png
eff6ce096e82a7339e3bfffecd916ec3.png

查看更多

关注者
0
被浏览
2.5k
雾月
雾月 2021-12-26
这家伙很懒,什么也没写!

\the<section> 后加一个 * 即可。

具体的,类似于:

\expandafter\def\expandafter\thesection\expandafter{\thesection*}

这里使用 \expandafter 是为了展开 \thesection 一次。
当然使用

\let\old@thesection\thesection
\def\thesection{\old@thesection*}

也可以。不过在使用之后必须立刻恢复 \thesection 的定义。

按照这个思路,可以写一个通用的宏 \starthis

\NewDocumentCommand \starthis { s m s o +m }
  {
    \group_begin:
    \exp_args:Nnf \cs_set:cpn { the \cs_to_str:N #2 } 
      { 
        \exp_after:wN \exp_after:wN \exp_after:wN 
        \exp_stop_f: \cs:w the \cs_to_str:N #2 \cs_end: * 
      }
    \IfBooleanT {#1}
      {
        \exp_args:Nnf \cs_set:cpn { CTEXthe \cs_to_str:N #2 } 
          { 
            \exp_after:wN \exp_after:wN \exp_after:wN 
            \exp_stop_f: \cs:w CTEXthe \cs_to_str:N #2 \cs_end: * 
          }
      }
    \IfBooleanTF {#3}
      { \IfValueTF {#4} { #2 * [#4] } { #2 * } }
      { \IfValueTF {#4} { #2   [#4] } { #2   } }
      {#5}
    \group_end:
  }
\ExplSyntaxOff

使用时只需

\starthis\chapter{章}

\starthis*\chapter{章}

\starthis\chapter[张]{章}

\starthis\section{节}

\starthis\subsection{小节}

* 的还会重定义 \CTEXthe<section>,以使得 \CTEXthe<section> 也有效。
对于完整的 \starthis\<section>*[...]{...} 也是支持的。

完整示例:

\documentclass{ctexart}
\usepackage{zhlipsum}
\usepackage[colorlinks]{hyperref}
\ExplSyntaxOn
\NewDocumentCommand \starthis { s m s o +m }
  {
    \group_begin:
    \exp_args:Nnf \cs_set:cpn { the \cs_to_str:N #2 } 
      { 
        \exp_after:wN \exp_after:wN \exp_after:wN 
        \exp_stop_f: \cs:w the \cs_to_str:N #2 \cs_end: \,* 
      }
    \IfBooleanT {#1}
      {
        \exp_args:Nnf \cs_set:cpn { CTEXthe \cs_to_str:N #2 } 
          { 
            \exp_after:wN \exp_after:wN \exp_after:wN 
            \exp_stop_f: \cs:w CTEXthe \cs_to_str:N #2 \cs_end: \,* 
          }
      }
    \IfBooleanTF {#3}
      { \IfValueTF {#4} { #2 * [#4] } { #2 * } }
      { \IfValueTF {#4} { #2   [#4] } { #2   } }
      {#5}
    \group_end:
  }
\ExplSyntaxOff

\begin{document}

\tableofcontents

\section{章一}

\begingroup
\expandafter\def\expandafter\thesubsection\expandafter{\thesubsection\,*}
\subsection{章一一}
\endgroup

\zhlipsum[1-2][name=zhufu]

\subsection{章一二}

\zhlipsum[4-6][name=zhufu]

\starthis\subsection{章一三}

\zhlipsum[7-9][name=zhufu]


\starthis\section{章二}

\subsection{章二一}

\zhlipsum[1-3][name=nanshanjing]

\starthis\subsection[章两两]{章二二}


\zhlipsum[4-7][name=nanshanjing]

\end{document}

image.png
image.png

1 个回答

撰写答案

请登录后再发布答案,点击登录

发布
问题

分享
好友

手机
浏览

扫码手机浏览