重新定义 \section 后 \ref 不打印任何内容

重新定义 \section 后 \ref 不打印任何内容

\ref重新定义 后,我遇到了问题\section。我正在使用提供给我的模板,之前他们正在更改 的定义,\thesection以使章节标题按要求显示。这反过来又搞乱了方程编号,因为我需要“section.equation”和“section.subsection.equation”方程编号(因此定义\thesection从这里传来)。相反,我返回\thesection到 中的默认定义,并为、和article.cls进行以下设置。\section\subsection\subsubsection

\def\section{\@ifstar\@mysection\@@mysection}
\def\subsection{\@ifstar\@mysubsection\@@mysubsection}
\def\subsubsection{\@ifstar\@mysubsubsection\@@mysubsubsection}
\newcommand{\@@mysection}[1]{%
    \vskip 24pt%
    \hspace{-3.5ex \@plus -1ex \@minus -.2ex}%
    \stepcounter{section}%
    \if@app%
        \bgroup%
        \Large\bfseries\Alph{section}. #1 \newline
        \egroup
        \numberwithin{equation}{section}
        \renewcommand{\theequation}{\Alph{section}.\arabic{equation}}
    \else%
        \def\thesection{\@arabic\c@section}%
        \bgroup%
        \Large\bfseries\@arabic\c@section. #1 \newline
        \egroup
        \numberwithin{equation}{section}
        \renewcommand{\theequation}{\arabic{section}.\arabic{equation}}
    \fi
}
\newcommand{\@mysection}[1]{%
    \vskip 24pt%
    \hspace{-3.5ex \@plus -1ex \@minus -.2ex}%
    \bgroup%
    \Large\bfseries #1 \newline
    \egroup
}
\newcommand{\@@mysubsection}[1]{%
    \vskip 18pt%
    \hspace{-3.5ex \@plus -1ex \@minus -.2ex}%
    \stepcounter{subsection}%
    \if@app%
        \bgroup%
        \large\bfseries\Alph{section}.\arabic{subsection}. #1 \newline
        \egroup
        \setcounter{equation}{0}
        \numberwithin{equation}{subsection}
        \renewcommand{\theequation{\Alph{section}.\arabic{subsection}.\arabic{equation}}
    \else %
        \bgroup%
        \large\bfseries\@arabic\c@section.\@arabic\c@subsection. #1 \newline
        \egroup
        \setcounter{equation}{0}
        \numberwithin{equation}{subsection}
        \renewcommand{\theequation}{\arabic{section}.\arabic{subsection}.\arabic{equation}}
    \fi 
}
\newcommand{\@mysubsection}[1]{%
    \vskip 24pt%
    \hspace{-3.5ex \@plus -1ex \@minus -.2ex}%
    \bgroup%
    \large\bfseries #1 \newline
    \egroup
}
\newcommand{\@@mysubsubsection}[1]{%
    \vskip 18pt%
    \hspace{-3.5ex \@plus -1ex \@minus -.2ex}%
    \stepcounter{subsubsection}%
    \if@app%
        \bgroup%
        \normalsize\bfseries\Alph{section}.\arabic{subsection}.\arabic{subsubsection}. #1 \newline
        \egroup
        \setcounter{equation}{0}
        \numberwithin{equation}{subsubsection}
        \renewcommand{\theequation}{\Alph{section}.\arabic{subsection}.\arabic{subsubsection}.\arabic{equation}}
    \else %
        \bgroup%
        \normalsize\bfseries\@arabic\c@section.\@arabic\c@subsection.\@arabic\c@subsubsection. #1 \newline
        \egroup
        \setcounter{equation}{0}
        \numberwithin{equation}{subsubsection}
        \renewcommand{\theequation}{\arabic{section}.\arabic{subsection}.\arabic{subsubsection}.\arabic{equation}}
    \fi 
}
\newcommand{\@mysubsubsection}[1]{%
    \vskip 24pt%
    \hspace{-3.5ex \@plus -1ex \@minus -.2ex}%
    \bgroup%
    \normalsize\bfseries #1 \newline
    \egroup
}

问题是,每当我用 引用节、小节或小子节时\ref,都不会打印任何内容。如果我使用\hyperref[sec:4]{4}(例如),它会起作用。我做错了什么?

答案1

据我所知,你的序言将要完成六件事:

  • 更改节、子节和子子节标题中使用的字体大小。但是,由于新字体大小 -- \Large\large\normalsize-- 是文档类的默认字体大小article,因此可以简单地省略相关说明。

  • 将附录中各节的节级标题的编号样式从阿拉伯数字更改为大写字母。如果\appendix在文档正文中遇到宏,则会自动执行此更改。

  • 在分节标题后自动缩进段落的第一行。通过加载indentfirst包可以很容易地实现这一点。

  • 更改公式的编号样式,始终在节、小节和小小节编号前加上前缀。

  • 将节、小节和小节标题上方的间距从弹性长度分别更改为固定长度24pt18pt18pt

  • 在相关标题中的章节、小节以及小节编号上添加一个“点”。

这些任务可以通过以下代码简洁地实现:

\documentclass[10pt]{article}
\usepackage{amsmath}
\usepackage{indentfirst} % indent first line after sectioning headers
\usepackage{etoolbox}    % for "\patchcmd" and "\pretocmd" macros
\usepackage[colorlinks]{hyperref} % hyperlinking of cross-references

%% Change equation numbering style
\pretocmd{\section}{\numberwithin{equation}{section}}{}{}
\pretocmd{\subsection}{\numberwithin{equation}{subsection}}{}{}
\pretocmd{\subsubsection}{\numberwithin{equation}{subsubsection}}{}{}

\makeatletter

%% Change spacing above sectioning headers
\patchcmd{\section}{-3.5ex \@plus -1ex \@minus -.2ex}{-24pt}{}{}
\patchcmd{\subsection}{-3.25ex\@plus -1ex \@minus -.2ex}{-18pt}{}{}
\patchcmd{\subsubsection}{-3.25ex\@plus -1ex \@minus -.2ex}{-18pt}{}{}

%% Affix "dot" to sectioning numbers
\def\@seccntformat#1{\@ifundefined{#1@cntformat}%
   {\csname the#1\endcsname\ }%       default
   {\csname #1@cntformat\endcsname}}% enable indiv. control
\newcommand\section@cntformat{\thesection.\ }
\newcommand\subsection@cntformat{\thesubsection.\ }
\newcommand\subsubsection@cntformat{\thesubsubsection.\ }

\makeatother

完整的 MWE (最小工作示例):

enter image description here

\documentclass[10pt]{article}
\usepackage{amsmath}
\usepackage{indentfirst} % indent first line after sectioning headers
\usepackage{etoolbox} % for "\patchcmd" and "\pretocmd" macros
\usepackage[colorlinks]{hyperref}

%% Change equation numbering style
\pretocmd{\section}{\numberwithin{equation}{section}}{}{}
\pretocmd{\subsection}{\numberwithin{equation}{subsection}}{}{}
\pretocmd{\subsubsection}{\numberwithin{equation}{subsubsection}}{}{}

\makeatletter
%% Change spacing above sectioning headers
\patchcmd{\section}{-3.5ex \@plus -1ex \@minus -.2ex}{-24pt}{}{}
\patchcmd{\subsection}{-3.25ex\@plus -1ex \@minus -.2ex}{-18pt}{}{}
\patchcmd{\subsubsection}{-3.25ex\@plus -1ex \@minus -.2ex}{-18pt}{}{}

%% Affix "dot" to sectioning numbers
\def\@seccntformat#1{\@ifundefined{#1@cntformat}%
   {\csname the#1\endcsname\ }%       default
   {\csname #1@cntformat\endcsname}}% enable indiv. control
\newcommand\section@cntformat{\thesection.\ }
\newcommand\subsection@cntformat{\thesubsection.\ }
\newcommand\subsubsection@cntformat{\thesubsubsection.\ }

\makeatother

\begin{document}
\section{Hello} \label{sec:hello}
\begin{equation}\label{eq:first} 1+1=2 \end{equation}

\subsection{Good morning} \label{sec:morning}
\begin{equation}\label{eq:second} 1+1=2 \end{equation}

\subsubsection{Good afternoon} \label{sec:afternoon}
\begin{equation}\label{eq:third} 1+1=2 \end{equation}

\appendix % <-- change numbering style of section-level headers

\section{Goodbye} \label{sec:goodbye}
\begin{equation}\label{eq:fourth} 1+1=2 \end{equation}

% Generate cross-references to sectioning headers and to equations:
\ref{sec:hello}, \ref{sec:morning}, \ref{sec:afternoon}, \ref{sec:goodbye}

\eqref{eq:first}, \eqref{eq:second}, \eqref{eq:third}, \eqref{eq:fourth}
\end{document}

相关内容