使用 autoref 进行交叉引用以实现自定义标题

使用 autoref 进行交叉引用以实现自定义标题

我通过定义新命令(参见乳胶代码)定制文档标题的外观(章节、节、小节等)。

但是,当我想使用命令对章节、节、子节等进行交叉引用时,\autoref只会得到“节”的结果,如下图所示。而我想要得到以下结果:

第1.1节

1.2.1 小节

ETC。

我确信问题来自于标题的重新定义,但是如何在保持相同配置的同时解决问题呢?

亲切的问候


\documentclass[a4paper,12pt,oneside,final,french]{book}

\headheight = 15pt 
\usepackage[margin=2.5cm, left=3cm, top=2.5cm, bottom=2.5cm]{geometry}

\usepackage[utf8]{inputenc} 
\usepackage[T1]{fontenc}

\usepackage{helvet} 
\renewcommand{\familydefault}{\sfdefault}


\usepackage{varioref}
\usepackage[pdftex,pdfborder={0 0 0},linkcolor =coolBlue,citecolor=coolRed,urlcolor=coolGreen]{hyperref}

%-----------------------Titles defining--------------------
\newcounter{poolChapter}
\newcounter{poolSection}[poolChapter]
\newcounter{poolSubSection}[poolSection]
\newcounter{poolSubSubSection}[poolSubSection]

\newcommand{\poolChapter}[1]{
    \newpage
    \stepcounter{poolChapter}
    \phantomsection 
    \addcontentsline{toc}{chapter}{\arabic{poolChapter}. #1}
    \begin{center}
        {   
            \null
            \vspace{-1.2\baselineskip}
            \vspace{4mm}
            \fontsize{16pt}{0}\selectfont
            \textbf{CHAPTER \arabic{poolChapter}}\\
            \vspace{7mm}
            \textbf{\uppercase{#1}}
        }
    \end{center}
}


\newcommand{\poolSection}[1]{
    \stepcounter{poolSection}
    \phantomsection
    \addcontentsline{toc}{section}{\arabic{poolChapter}.\arabic{poolSection}. #1}
    {\fontsize{14pt}{0}\selectfont \textbf{\arabic{poolChapter}.\arabic{poolSection}. #1}}
}

\newcommand{\poolSubSection}[1]{
    \stepcounter{poolSubSection}
    \phantomsection
    \addcontentsline{toc}{subsection}{\arabic{poolChapter}.\arabic{poolSection}.\arabic{poolSubSection}. #1}
    {\textbf{\arabic{poolChapter}.\arabic{poolSection}.\arabic{poolSubSection}. #1}}
}

\newcommand{\poolSubSubSection}[1]{
    \stepcounter{poolSubSubSection}
    \phantomsection
    \addcontentsline{toc}{subsubsection}{\arabic{poolChapter}.\arabic{poolSection}.\arabic{poolSubSection}.\arabic{poolSubSubSection}. #1}
    {\textbf{\arabic{poolChapter}.\arabic{poolSection}.\arabic{poolSubSection}.\arabic{poolSubSubSection}. #1}}
}


\newcommand{\poolDummyChapter}[1]{
    \newpage
  
    \begin{center}
        {   
            \null
            \vspace{-1.6\baselineskip}
            \vspace{1.4cm}
            \large\textbf{#1}
        }
    \end{center}
}


%-----------------------------------------------------------------


\begin{document}
\poolChapter{Materials and methods}

\poolSection{Materials}

See \autoref{methods}

\poolSection{Methods} \label{methods}

See \autoref{method1}

\poolSubSection{Method 1} \label{method1}

\end{document}

示例渲染

答案1

不要使用\phantomsectionbut \refstepcounter,并定义自动引用名称:

\documentclass[a4paper,12pt,oneside,final,french]{book}

\headheight = 15pt
\usepackage[margin=2.5cm, left=3cm, top=2.5cm, bottom=2.5cm]{geometry}

\usepackage[T1]{fontenc}

\usepackage{helvet}
\renewcommand{\familydefault}{\sfdefault}


\usepackage{varioref}
\usepackage[pdfborder={0 0 0},linkcolor =coolBlue,citecolor=coolRed,urlcolor=coolGreen]{hyperref}

%-----------------------Titles defining--------------------
\newcounter{poolChapter}
\newcounter{poolSection}[poolChapter]
\newcounter{poolSubSection}[poolSection]
\newcounter{poolSubSubSection}[poolSubSection]

\newcommand{\poolChapter}[1]{
    \newpage
    \refstepcounter{poolChapter}
    %\phantomsection
    \addcontentsline{toc}{chapter}{\arabic{poolChapter}. #1}
    \begin{center}
        {
            \null
            \vspace{-1.2\baselineskip}
            \vspace{4mm}
            \fontsize{16pt}{0}\selectfont
            \textbf{CHAPTER \arabic{poolChapter}}\\
            \vspace{7mm}
            \textbf{\uppercase{#1}}
        }
    \end{center}
}


\newcommand{\poolSection}[1]{
    \refstepcounter{poolSection}
    %\phantomsection
    \addcontentsline{toc}{section}{\arabic{poolChapter}.\arabic{poolSection}. #1}
    {\fontsize{14pt}{0}\selectfont \textbf{\arabic{poolChapter}.\arabic{poolSection}. #1}}
}

\newcommand{\poolSubSection}[1]{
    \refstepcounter{poolSubSection}
    %\phantomsection
    \addcontentsline{toc}{subsection}{\arabic{poolChapter}.\arabic{poolSection}.\arabic{poolSubSection}. #1}
    {\textbf{\arabic{poolChapter}.\arabic{poolSection}.\arabic{poolSubSection}. #1}}
}

\newcommand{\poolSubSubSection}[1]{
    \refstepcounter{poolSubSubSection}
    %\phantomsection
    \addcontentsline{toc}{subsubsection}{\arabic{poolChapter}.\arabic{poolSection}.\arabic{poolSubSection}.\arabic{poolSubSubSection}. #1}
    {\textbf{\arabic{poolChapter}.\arabic{poolSection}.\arabic{poolSubSection}.\arabic{poolSubSubSection}. #1}}
}


\newcommand{\poolDummyChapter}[1]{
    \newpage

    \begin{center}
        {
            \null
            \vspace{-1.6\baselineskip}
            \vspace{1.4cm}
            \large\textbf{#1}
        }
    \end{center}
}

\newcommand\poolSectionautorefname{Pool section}
\newcommand\poolSubSectionautorefname{Pool subsection}

%-----------------------------------------------------------------


\begin{document}
\poolChapter{Materials and methods}

\poolSection{Materials}

See \autoref{methods}

\poolSection{Methods} \label{methods}

See \autoref{method1}

\poolSubSection{Method 1} \label{method1}

\end{document}

在此处输入图片描述

要获得不同的标签,请重新定义计数器表示,例如\thepoolSubSection

\renewcommand\thepoolSubSection{\arabic{poolChapter}.\arabic{poolSection}.\arabic{poolSubSection}}

请注意,您的定义不会提供真正的分段命令:它们不会在下一行之前抑制分页符,也不会设置标题。通常,最好创建新的分段变体,\@startsection然后处理此类细节。

相关内容