我想要几种类型的 \sections。\starSection{Title} 将打印“1.3 Title”,但 \starSection[2]{Title} 将打印“1.3** Title”。
你可以帮帮我吗?
谢谢
编辑
实际上,我希望有两个这样的计数器。一个生产1.3\,** Title
,另一个生产1.3\,$^{\displaystyle\textbf{!!}}$
。
\gdef\mystars{}
\gdef\myexclmarks{}
\renewcommand{\thesubsection}{\arabic{section}.\arabic{subsection}\mystars\myexclmarks\setImportance{0}\setStars{0}}
\newcounter{secstars}
\newcommand\setStars[1]{%
\gdef\mystars{}%
\ifnum#1=0%
\else
\setcounter{secstars}{0}%
\loop\edef\mystars{*\mystars}%
\stepcounter{secstars}%
\ifnum\value{secstars}<#1\repeat
\edef\mystars{\,\mystars}
\fi%
}
\newcounter{secexclmarks}
\newcommand\setImportance[1]{%
\gdef\myexclmarks{}%
\ifnum#1=0%
\else
\def\myexclmarksaux{}%
\setcounter{secexclmarks}{0}%
\loop\edef\myexclmarksaux{!\myexclmarksaux}%
\stepcounter{secexclmarks}%
\ifnum\value{secexclmarks}<#1\repeat
\gdef\myexclmarks{\,$^{\displaystyle\textbf{\myexclmarksaux}}$}
\fi%
}
答案1
请注意,原始\section
命令确实有一个可选参数(它决定了条目在目录中的外观)。假设您决定不使用它,您可以这样做
\documentclass{article}
\newcounter{secstars}
\newcommand\starSection[2][0]{%
\begingroup\def\mystars{}%
\ifnum#1=0%
\else
\setcounter{secstars}{0}%
\loop\edef\mystars{*\mystars}%
\stepcounter{secstars}%
\ifnum\value{secstars}<#1\repeat
\fi%
\renewcommand{\thesection}{\arabic{section}\mystars}%
\section{#2}%
\endgroup}
\begin{document}
\starSection[3]{abc}
\starSection[2]{xyz}
\section{test}
\starSection{uvw}
\end{document}
如果您不能排除使用可选条目的可能性,我建议您采用以下更为灵活的替代方案。
\documentclass{article}
\def\mystars{}%
\renewcommand{\thesection}{\arabic{section}\mystars}%
\newcounter{secstars}
\newcommand\setstars[1]{%
\def\mystars{}%
\ifnum#1=0%
\else
\setcounter{secstars}{0}%
\loop\xdef\mystars{*\mystars}%
\stepcounter{secstars}%
\ifnum\value{secstars}<#1\repeat
\fi%
}
\begin{document}
\setstars{3}
\section{abc}
\setstars{2}
\section[$xyz$]{\boldmath$xyz$\unboldmath}
\setstars{0}
\section{test}
\end{document}
这一切都假设您不想使用titlesec
提供更多高级选项的软件包。
答案2
注意:根据问题中的更多信息,重新制定了答案
我建议对不同类型的部分使用不同的名称;借助它,\@seccntformat
我们可以将我们想要的内容附加到部分编号中。
\documentclass{article}
\usepackage{xparse}
\let\latexsection\section
\ExplSyntaxOn
\NewDocumentCommand{\genericsection}{m m D(){0} s O{#6} m}
{% #1 = formatting command
% #2 = symbol
% #3 = rep
% #4 = * variant
% #5 = optional short title (default #6)
% #6 = title
\tl_set:Nx \l_colas_symbol_tl { \prg_replicate:nn { #3 } { #2 } }
\tl_if_empty:NTF \l_colas_symbol_tl
{
\cs_set_protected:cpn { section@append } { }
}
{
\cs_set_protected:cpn { section@append } { #1{\l_colas_symbol_tl} }
}
\IfBooleanTF{#4}
{
\latexsection*{#5}
}
{
\latexsection[#5]{#6}
}
}
\tl_new:N \l_colas_symbol_tl
\ExplSyntaxOff
\makeatletter
\renewcommand{\@seccntformat}[1]{%
\csname the#1\endcsname\csname #1@append\endcsname\quad
}
\makeatother
\newcommand{\dsection}{\genericsection{}{*}}
\newcommand{\isection}{\genericsection{\textsuperscript}{!}}
\renewcommand{\section}{\genericsection{}{}}
\begin{document}
\tableofcontents
\section*{Starred section}
\section{Normal section}
\dsection(1){Difficult section}
\dsection(2){Very hard section}
\isection(2){Very important section}
\isection(4){Very very important section}
\section{Another normal section}
\end{document}
答案3
问题是你希望这些感叹号和/或星星出现在哪个位置:
- 在正文的章节标题中?(似乎很明显。)
- 在目录中重复的章节标题中?
- 在页面标题中使用
\pagestyle{headings}
? \ref
当通过?引用相应的切片编号时\autoref
当通过?引用相应的切片编号时- 在您的 PDF 查看程序的书签窗口/内容窗口中列出的重复章节标题中?
- ...
这些感叹号和/或星号只出现在正文的章节标题中的起点可能是一种“机制”,其中将一个用于将内容附加到结果的钩子\thesection
添加到\thesection
宏中。该钩子通常是空的,但可以暂时重新定义为提供任意数量的星号和/或感叹号。当在\thesection
重新定义生效期间执行时,您将获得这些星号/感叹号。
在下面的示例中,钩子由宏组成\AppendToNumber
,
您还可以使用宏,这些宏将\section
-commands 作为参数,并为您执行钩子宏所需的临时重新定义。
在下面的示例中,这些是宏\difficult
和\important
。
在下面的例子中,我使用了 David Kastrup 的\replicate
宏来发明一个基于扩展的宏,用于产生一定数量的星号/感叹号,而无需使用/浪费计数器/\count
寄存器,也不需要大量临时宏分配。
我不提供任何担保/保证。
我不知道哪些附加包可能会破坏这一点。
\documentclass{article}
\usepackage{hyperref}
\begingroup
\makeatletter
\@firstofone{%
\endgroup
%------------------------------------------------------------------------------
% David Kastrup's replicate, see
% http://www.gust.org.pl/projects/pearls/2005p/david-kastrup/bachotex2005-david-kastrup-pearl3.pdf
% \replicate{<number>}{<tokens>}
% e.g., \replicate{3}{XYZ} -> XYZXYZXYZ
%------------------------------------------------------------------------------
\newcommand\xii[2]{\if#2m#1\expandafter\xii\else\expandafter\@gobble\fi{#1}}
\@ifdefinable\xiii{\long\def\xiii#1\relax#2{\xii{#2}#1\relax}}
\newcommand\replicate[1]{\expandafter\xiii\romannumeral\number\number#1 000\relax}
%------------------------------------------------------------------------------
% Print an amount of stars:
%------------------------------------------------------------------------------
\@ifdefinable\StarsLoop{%
\DeclareRobustCommand\StarsLoop[1]{%
\ifnum#1=0 \expandafter\@gobble\else\expandafter\@firstofone\fi
{%
\@ifundefined{texorpdfstring}{\@firstoftwo}{\texorpdfstring}%
{\,}{ }%
\replicate{#1}{*}%
}%
}%
}%
%------------------------------------------------------------------------------
% Print an amount of Exclamation-marks:
%------------------------------------------------------------------------------
\@ifdefinable\ExclamationMarksLoop{%
\DeclareRobustCommand\ExclamationMarksLoop[1]{%
\ifnum#1=0 \expandafter\@gobble\else\expandafter\@firstofone\fi
{%
\@ifundefined{texorpdfstring}{\@firstoftwo}{\texorpdfstring}%
{\,\textsuperscript}{ \@firstofone}{\replicate{#1}{!}}%
}%
}%
}%
%------------------------------------------------------------------------------
% Add a hook \AppendToNumber to \thesection for appending tokens to a section number:
%------------------------------------------------------------------------------
\newcommand\AppendToNumber{}%
\renewcommand\thesection{\arabic{section}\noexpand\AppendToNumber}%
% Usage of \noexpand instead of \protect causes stars/exclamation-marks to
% end up in the data for referencing-labels also.
%------------------------------------------------------------------------------
% \difficult and \important:
%------------------------------------------------------------------------------
\newcommand\difficult[2]{%
\expandafter\def\expandafter\AppendToNumber
\expandafter{\AppendToNumber\StarsLoop{#1}}%
#2%
\let\AppendToNumber=\@empty
}%
\newcommand\important[2]{%
\expandafter\def\expandafter\AppendToNumber
\expandafter{\AppendToNumber\ExclamationMarksLoop{#1}}%
#2%
\let\AppendToNumber=\@empty
}%
}%-that closes \@firstofone's argument.
\pagestyle{headings}
\begin{document}
\tableofcontents
\newpage
\section*{Starred section}
\section{Normal section}
\difficult{1}{\section{Difficult section}}
\newpage
\difficult{8}{%
\section{A nicely challenging section}\label{InsideDifficult}%
}\label{OutsideDifficult}
\important{2}{\section{Very important section}}
\important{4}{\section{Very very important section}}
\difficult{2}{\important{2}{\section{Very very difficult and very very important section}}}
\section{Another normal section}
\vfill\noindent\textbf{Referencing:}\bigskip
\noindent You can choose whether to have the referenced number with/without stars/exclamation-marks
by placing the corresponding referencing-label inside/behind the second argument of
\verb|\difficult|/\verb|\important|.\bigskip
\noindent\verb|\ref{InsideDifficult}|: \ref{InsideDifficult}
\noindent\verb|\ref{OutsideDifficult}|: \ref{OutsideDifficult}
\noindent\verb|\autoref{InsideDifficult}|: \autoref{InsideDifficult}
\noindent\verb|\autoref{OutsideDifficult}|: \autoref{OutsideDifficult}
\vfill
\end{document}