为什么 \numberwithin{figure}{chapter} 在附录中不起作用?

为什么 \numberwithin{figure}{chapter} 在附录中不起作用?

我想使用

\numberwithin{figure}{section}用于正文

\numberwithin{figure}{chapter}附录

但在 Appenix 中它不能很好地工作,看起来像这样:

在此处输入图片描述

更奇怪的是,

当我将其用于 \numberwithin{figure}{chapter}正文时

以及 \numberwithin{figure}{section}附录

它们都运行良好...

有人能帮我吗?

测试的 MWE:

\documentclass[%
    a4paper,% not needed because default       
    oneside,        
    12pt,               
    onecolumn,          
    openright,% nonsense after oneside or before openay
    openany,% nonsense after onecolumn
    parskip=full*,      
    headsepline,        
    footsepline,        
    bibliography=totoc,
    numbers=noenddot,   
    appendixprefix
]{scrbook}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{scrlayer-scrpage}
\usepackage{amsmath}
\usepackage{blindtext}

%---------------------------------------------------------
%\renewcommand\thefigure{\thesection.\arabic{figure}}
\numberwithin{figure}{section}
%---------------------------------------------------------
\begin{document}

\chapter{chapter}

\section{section1}

 \begin{figure}[h]
    \includegraphics*{testfig.png}
    \caption{Fig for Test}
 \end{figure}

\section{section2}

 \begin{figure}[h]
    \includegraphics*{testfig.png}
    \caption{Fig for Test}
 \end{figure}


\appendix
%---------------------------------------------------------
%\renewcommand\thefigure{\thechapter.\arabic{figure}}
\numberwithin{figure}{chapter}
%---------------------------------------------------------

\chapter{the first chapter in appendix}


\section{first sec in appendix}

 \begin{figure}[h]
    \includegraphics*{testfig.png}
    \caption{Fig for Test}
 \end{figure}

\section{second sec in appendix}

 \begin{figure}[h]
    \includegraphics*{testfig.png}
    \caption{Fig for Test}
 \end{figure}


\end{document}

答案1

该问题与附录或 KOMA-Script 无关:

\documentclass{book}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{blindtext}

\numberwithin{figure}{section}
\begin{document}

\chapter{chapter}

\section{section1}

\begin{figure}[h]
  \includegraphics*{example-image}
  \caption{Fig for Test}
\end{figure}

\section{section2}

\begin{figure}[h]
  \includegraphics*{example-image}
  \caption{Fig for Test}
\end{figure}

\numberwithin{figure}{chapter}

\chapter{another chapter}

\section{first sec after number change}

\begin{figure}[h]
  \includegraphics*{example-image}
  \caption{Fig for Test}
\end{figure}

\section{second after number change}

\begin{figure}[h]
  \includegraphics*{example-image}
  \caption{Fig for Test}
\end{figure}

\end{document}

\numberwithin不会从上一个 的父计数器的重置列表中删除计数器\numberwithin。因此\section命令仍然会重置figure计数器。您必须自己将其删除。您可以使用包chngcntr

\documentclass{book}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{amsmath}
\usepackage{blindtext}
\usepackage{chngcntr}

\counterwithin{figure}{section}
\begin{document}

\chapter{chapter}

\section{section1}

\begin{figure}[h]
  \includegraphics*{example-image}
  \caption{Fig for Test}
\end{figure}

\section{section2}

\begin{figure}[h]
  \includegraphics*{example-image}
  \caption{Fig for Test}
\end{figure}

\counterwithout{figure}{section}
\counterwithin{figure}{chapter}

\chapter{another chapter}

\section{first sec after number change}

\begin{figure}[h]
  \includegraphics*{example-image}
  \caption{Fig for Test}
\end{figure}

\section{second after number change}

\begin{figure}[h]
  \includegraphics*{example-image}
  \caption{Fig for Test}
\end{figure}

\end{document}

或者您可以使用 KOMA-Script 的\@removefromreset

\documentclass[%
    a4paper,% not needed because default       
    oneside,        
    12pt,               
    onecolumn,          
    openright,% nonsense after oneside or before openay
    openany,% nonsense after onecolumn
    parskip=full*,      
    headsepline,        
    footsepline,        
    bibliography=totoc,
    numbers=noenddot,   
    appendixprefix
]{scrbook}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{graphicx}
\usepackage{scrlayer-scrpage}
\usepackage{amsmath}
\usepackage{blindtext}

%---------------------------------------------------------
%\renewcommand\thefigure{\thesection.\arabic{figure}}
\numberwithin{figure}{section}
%---------------------------------------------------------
\begin{document}

\chapter{chapter}

\section{section1}

 \begin{figure}[h]
    \includegraphics*{example-image}
    \caption{Fig for Test}
 \end{figure}

\section{section2}

 \begin{figure}[h]
    \includegraphics*{example-image}
    \caption{Fig for Test}
 \end{figure}


\appendix
%---------------------------------------------------------
%\renewcommand\thefigure{\thechapter.\arabic{figure}}
\csname @removefromreset\endcsname{figure}{section}% added
\numberwithin{figure}{chapter}
%---------------------------------------------------------

\chapter{the first chapter in appendix}


\section{first sec in appendix}

 \begin{figure}[h]
    \includegraphics*{example-image}
    \caption{Fig for Test}
 \end{figure}

\section{second sec in appendix}

 \begin{figure}[h]
    \includegraphics*{example-image}
    \caption{Fig for Test}
 \end{figure}

\end{document}

相关内容