免費論壇 繁體 | 簡體
Sclub交友聊天~加入聊天室當版主
分享
返回列表 发帖

请教例题编号时,能不能有多个层级?

如题。我打了一些例题,一般都是这样的层级:chapter -> section -> example,然后在定义时是这样的:
\newtheorem{example}{例}[section]

但有少数例题,就直接在chapter后面:chapter->example,这时编号就会像这样:例 1.0.1,表示第1章第1个例题,因为它没有section所以section变成0了,那能不能让它显示成“例1.1”这样呢?就是如果没有section时,就直接用chapter的编号加例题的编号
分享到: QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友

就是要去掉 .0 的 section ?那用个条件判断就好了
  1. \documentclass[UTF8]{ctexbook}
  2. \usepackage{amsthm}
  3. \newtheorem{example}{例}[section]
  4. \makeatletter
  5. \renewcommand\theexample{%
  6. \ifnum\c@section=0\relax
  7. \thechapter.\arabic{example}%
  8. \else
  9. \thesection.\arabic{example}%
  10. \fi}
  11. \makeatother
  12. \begin{document}
  13. \chapter{A}
  14. \begin{example}
  15. 无节
  16. \end{example}
  17. \begin{example}
  18. 无节
  19. \end{example}
  20. \section{A-a}
  21. \begin{example}
  22. 有节
  23. \end{example}
  24. \begin{example}
  25. 有节
  26. \end{example}
  27. \section{A-b}
  28. \begin{example}
  29. 有节
  30. \end{example}
  31. \begin{example}
  32. 有节
  33. \end{example}
  34. \end{document}
复制代码
$\href{https://kuingggg.github.io/}{\text{About Me}}$

TOP

回复 2# kuing

如果还需要处理其他的,比如引理定理命题什么的,每个都这样写就太麻烦了,干脆整一个命令来:
  1. \documentclass[UTF8]{ctexbook}
  2. \usepackage{amsthm}

  3. \makeatletter
  4. \newcommand\rmzosec[1]{%remove zero section
  5. \expandafter\renewcommand\csname the#1\endcsname{%
  6. \ifnum\c@section=0\relax
  7. \thechapter.\arabic{#1}%
  8. \else
  9. \thesection.\arabic{#1}%
  10. \fi}}
  11. \makeatother

  12. \newtheorem{example}{例}[section]
  13. \newtheorem{thm}{定理}[section]
  14. \rmzosec{example}
  15. \rmzosec{thm}

  16. \begin{document}
  17. \chapter{A}
  18. \begin{thm}
  19. 无节
  20. \end{thm}
  21. \begin{thm}
  22. 无节
  23. \end{thm}
  24. \begin{example}
  25. 无节
  26. \end{example}
  27. \begin{example}
  28. 无节
  29. \end{example}
  30. \section{A-a}
  31. \begin{thm}
  32. 有节
  33. \end{thm}
  34. \begin{thm}
  35. 有节
  36. \end{thm}
  37. \begin{example}
  38. 有节
  39. \end{example}
  40. \begin{example}
  41. 有节
  42. \end{example}
  43. \section{A-b}
  44. \begin{thm}
  45. 有节
  46. \end{thm}
  47. \begin{thm}
  48. 有节
  49. \end{thm}
  50. \begin{example}
  51. 有节
  52. \end{example}
  53. \begin{example}
  54. 有节
  55. \end{example}
  56. \end{document}
复制代码
$\href{https://kuingggg.github.io/}{\text{About Me}}$

TOP

回复 2# kuing

谢谢,这个就很好用了,目前只有例题有这种情况,像引入似的举了几个例子,然后后面就进入分节了。

TOP

回复 2# kuing

我突然发现这个新的,好像中间空了很大。
无标题.gif

前两个都是第0章无节的命题1,第一个没用这楼的设置,第二个用了这楼的设置,觉得汉字和数字之间的空白好像变大了。
后两个都是第1章第1节的例4,第一个没用设置,第二个用了设置,也是那个“例”和后面的数字之间的空白变大了。

这个能调吗?

TOP

回复 5# abababa

我用 3# 的代码来测试并没有出现这个问题,你用 3# 的整段代码试试看有没有问题,如果没有,那你给一段能重现问题的最小代码。

TOP

回复 6# kuing
我用的是二楼的那个,示例代码如下,如果把那个“去掉无 section 的环境编号 0”下边那段用iffalse注释掉和不注释掉,就出现5楼的现象。
  1. \documentclass[a4paper]{book}
  2. \usepackage{amsmath,bm,yhmath,amssymb,etoolbox,esvect,stmaryrd,cite,enumitem,extarrows,mathtools,ifthen}
  3. \usepackage{etoolbox}
  4. %定理环境
  5. \usepackage[amsmath, thmmarks]{ntheorem}
  6. {
  7.         \theoremstyle{nonumberplain}
  8.         \theoremheaderfont{\indent\bfseries}
  9.         \theorembodyfont{\normalfont}
  10.         \theoremsymbol{\ensuremath{\Box}}
  11.         \newtheorem{proof}{证明}
  12. }
  13. \newtheorem{example}{例}[section]
  14. \newcommand\exampleautorefname{例}
  15. % 去掉无 section 的环境编号 0

  16. \makeatletter
  17. \renewcommand\theexample{
  18.         \ifnum\c@section=0\relax
  19.         \thechapter.\arabic{example}
  20.         \else
  21.         \thesection.\arabic{example}
  22.         \fi
  23. }
  24. \makeatother
  25. \iffalse
  26. \fi

  27. \usepackage[pdfencoding=auto,psdextra,colorlinks,linkcolor=red,anchorcolor=blue,citecolor=blue]{hyperref}
  28. % 字体
  29. \usepackage[slantfont,boldfont]{xeCJK}
  30. \punctstyle{kaiming}

  31. \begin{document}
  32. \chapter{first}
  33. \begin{example}\label{exam01}
  34. example01
  35. \end{example}

  36. ref\autoref{exam01}
  37. \end{document}
复制代码

TOP

回复 7# abababa

那是因为你改了我的代码——请把你以为没用的三个 % 给补回去。

TOP

回复 8# kuing

原来如此,我还以为注释后面什么也没有,就相当于没注释,没用呢。这是什么原理呢?

TOP

回复 9# abababa
  1. \documentclass{article}
  2. \begin{document}
  3. xx
  4. yy%
  5. zz
  6. \end{document}
复制代码

TOP

回复 10# kuing

这是把那个换行符注释掉了吗,如果把那个命令写成一行,效果是一样的:
  1. \renewcommand\theexample{\ifnum\c@section=0\relax\thechapter.\arabic{example}\else\thesection.\arabic{example}\fi
复制代码
所以猜是把换行注释了。

TOP

回复 11# abababa

是的,单个回车 = 空格。

不过,如果行末是命令,就没这问题,因为回车被命令吃掉了,所以我那代码就只有那三个地方需要 %

将 10# 的例子改成
  1. \documentclass{article}
  2. \newcommand\xx{xx}
  3. \newcommand\yy{yy}
  4. \newcommand\zz{zz}
  5. \begin{document}
  6. \xx
  7. \yy
  8. \zz
  9. \end{document}
复制代码

TOP

回复 12# kuing
这个不可见字符以前一直没注意,有些latex的文档里就有很多空白的%,以前还觉得奇怪,也没有注释加上有什么用,原来就是做这个用的。

TOP

返回列表 回复 发帖