使用elegantbook模板如何设置「示例类环境」example的计数器为「按章、节两级标题」计数?

发布于 2025-07-17 23:42:56

行列式.pdf
这是我作品

Edited by Sagittarius Rover

原问题的MWE如下

\documentclass[lang=cn,11pt,twoside,openany]{elegantbook}
\geometry{paperheight=10cm}
\begin{document}%

\chapter{test}
\section{sec}

\begin{example}
test test test test
\end{example}

\begin{example}
    test test test test
\end{example}

\section{sec}

\begin{example}
    test test test test
\end{example}

\chapter{test}
\section{sec}

\begin{example}
    test test test test
    \end{example}
    
    \begin{example}
        test test test test
    \end{example}
    
    \section{sec}
    
    \begin{example}
        test test test test
    \end{example}
\end{document}  

image.png

这样得到的「example」按照文档所述是「按chapter」编号

image.png

如何修改为「按章、节两层计数器」编号呢?

查看更多

关注者
0
被浏览
54
1 个回答
Sagittarius Rover
Sagittarius Rover 6小时前
我要成为Typst糕手/(ㄒoㄒ)/~~

注意,本站不是「PDF分享」站

提问就要focus在真正体现你问题的关键点上,给可以体现你问题的示例代码

我帮你在原问题里补充了「最小工作示例」,希望你下次提问能自己给出

image.png


也不太难,看下原码即可:

% elegantbook.cls Line 1165
%% Example with counter
\newcounter{exam}[chapter]
\setcounter{exam}{0}
\renewcommand{\theexam}{\thechapter.\arabic{exam}}
\newenvironment{example}[1][]{
  \refstepcounter{exam}
  \par\noindent\textbf{\color{main}{\examplename} \theexam #1 }\rmfamily}{
  \par\ignorespacesafterend}

想办法把上面的设置覆盖掉即可...

\documentclass[lang=cn,11pt,twoside,openany]{elegantbook}
\geometry{paperheight=10cm}
\counterwithin{exam}{section}
\renewcommand{\theexam}{\thesection.\arabic{exam}}
\begin{document}%

\chapter{test}
\section{sec}

\begin{example}
test test test test
\end{example}

\begin{example}
    test test test test
\end{example}

\section{sec}

\begin{example}
    test test test test
\end{example}

\chapter{test}
\section{sec}

\begin{example}
    test test test test
    \end{example}
    
    \begin{example}
        test test test test
    \end{example}
    
    \section{sec}
    
    \begin{example}
        test test test test
    \end{example}
\end{document}  

image.png

注意:这样只修改了example环境,对于同样属于「示例类环境」的problemexercise环境的修改,也是类似的...留做习题...

撰写答案

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

发布
问题

分享
好友

手机
浏览

扫码手机浏览