对练习/定理的内部引用(而不是对章节的引用)

对练习/定理的内部引用(而不是对章节的引用)

我想引用一个练习/定理,它在我的文档中用我自己的计数器编号。类似于:正如我们在定理 6.4.3 中看到的,...

我的问题是我不想使用章节号,而是使用练习/定理的编号。所以在我看来,\label\ref命令似乎不起作用。我发现这个链接非常有趣:

http://www.ryancreich.info/index.php?page=latex&subpage=rrthm

然而,我无法适应这一点 - 恐怕还不够好 - 并且我明确表示我没有使用 AMS 定理环境,因为我定义了自己的。

以下是一个简短的示例:

\documentclass{book}[11pt,a4paper]
\usepackage[utf8]{inputenc}
\usepackage[OT1]{fontenc}
\usepackage{amsmath}
\newcounter{myCounter}
\setcounter{myCounter}{1}
\newcommand{\exercise}[1]{\textbf {Exercise \thechapter.\themyCounter }

    #1 \stepcounter{myCounter}
}
\parindent=0pt

\begin{document}
    \chapter{First chapter}

    \exercise{First exercise}

    \exercise{Second exercise}

    \exercise{Third exercise}

    Here I would like to make a reference to second exercise that would print ”Exercise 2.1" but would change whenever I add/remove an exercise/theorem before the given exercise.

    Something like : as we saw in "Exercise 2.1", the sum of integers from 1 to n is $\dfrac{n(n+1)}{2}$

\end{document}

希望我的问题清楚。

答案1

这是一种方法。但是,我建议使用类似定理的环境。

\documentclass[11pt, a4paper]{book}
\usepackage[utf8]{inputenc}
\usepackage[OT1]{fontenc}
\usepackage{amsmath}
\usepackage{chngcntr}
\newcounter{myCounter}
\counterwithin{myCounter}{chapter}
\setcounter{myCounter}{0}
\newcommand{\exercise}[1]{\refstepcounter{myCounter}\textbf {Exercice \themyCounter }
    #1
}
\parindent=0pt

\begin{document}

    \chapter{First chapter}

    \exercise{First exercise\label{exo1}}

    \exercise{Second exercise\label{exo2}}

    \exercise{Third exercise\label{exo3}}

    Here I would like to make a reference to second exercice that would print ”Exercice 2.1" but would change whenever I add/remove an exercice/theorem before the given exercice.

    Something like : as we saw in "Exercice \ref{exo2}", the sum of integers from 1 to n is $\dfrac{n(n+1)}{2}$

\end{document} 

在此处输入图片描述

答案2

\documentclass{book}[11pt,a4paper]
\usepackage[utf8]{inputenc}
\usepackage[OT1]{fontenc}
\usepackage{amsmath}
\usepackage{hyperref}
\usepackage{deflist}

\newcounter{myCounter}[chapter]
\renewcommand\themyCounter{\thechapter.\arabic{myCounter}}
\newcommand\myCounterautorefname{\MyUppercasefork exercise}

\makeatletter
\newif\ifMyUppercase
\MyUppercasefalse
\DeclareRobustCommand\MyUppercasefork[1]{%
  \ifMyUppercase\expandafter\MakeUppercase\else\expandafter\@firstofone\fi{#1}%
}%

\newcommand{\exercise}[1]{%
  \begin{deflist}{\textbf{Exercise 99.999}}%
  \refstepcounter{myCounter}%
  \protected@edef\@currentlabelname{%
    \myCounterautorefname\protect\space\themyCounter
  }%
  \item[{%
    \textbf{{\MyUppercasetrue\@currentlabelname}}%
   }]#1%
  \end{deflist}%
}%
\makeatother

\parindent=0pt

\begin{document}

\tableofcontents

\chapter{First chapter}
\enlargethispage{2cm}

\section*{References with clickable hyperlinks}

\verb|\ref{Ex2.1}|: \ref{Ex2.1}

\verb|\pageref{Ex2.1}|: \pageref{Ex2.1}

\subsection*{\texorpdfstring{\protect\texttt}{}%
             {\textbackslash MyUppercasefalse}:}
\MyUppercasefalse

\verb|\href{\string#myCounter.2.1}{A nice long link to \autoref*{Ex2.1}}|:\\
\href{\string#myCounter.2.1}{A nice long link to \autoref*{Ex2.1}}
\smallskip

\verb|\hyperref[Ex2.1]{A nice long link to \autoref*{Ex2.1}}|:\\
\hyperref[Ex2.1]{A nice long link to \autoref*{Ex2.1}}
\smallskip

\verb|\nameref{Ex2.1}|: \nameref{Ex2.1}

\verb|\autoref{Ex2.1}|: \autoref{Ex2.1}

\subsection*{\texorpdfstring{\protect\texttt}{}%
             {\textbackslash MyUppercasetrue}:}
\MyUppercasetrue

\verb|\nameref{Ex2.1}|: \nameref{Ex2.1}

\verb|\autoref{Ex2.1}|: \autoref{Ex2.1}

\MyUppercasefalse

\section*{References without clickable hyperlinks}

\verb|\ref*{Ex2.1}|: \ref*{Ex2.1}

\verb|\pageref*{Ex2.1}|: \pageref*{Ex2.1}

\subsection*{\texorpdfstring{\protect\texttt}{}%
             {\textbackslash MyUppercasefalse}:}
\MyUppercasefalse

\verb|\nameref*{Ex2.1}|: \nameref*{Ex2.1}

\verb|\autoref*{Ex2.1}|: \autoref*{Ex2.1}

\subsection*{\texorpdfstring{\protect\texttt}{}%
             {\textbackslash MyUppercasetrue}:}
\MyUppercasetrue

\verb|\nameref*{Ex2.1}|: \nameref*{Ex2.1}

\verb|\autoref*{Ex2.1}|: \autoref*{Ex2.1}

\MyUppercasefalse

\chapter{Second chapter}

\exercise{%
  \label{Ex2.1}First exercise of second chapter.
  First exercise of second chapter.
  First exercise of second chapter.
  First exercise of second chapter.
  First exercise of second chapter.
  First exercise of second chapter.
}

\exercise{Second exercise of second chapter.}

\exercise{Third exercise of second chapter.}

\exercise{Fourth exercise of second chapter.}

\exercise{Fifth exercise of second chapter.}

\exercise{Sixth exercise of second chapter.}

\exercise{Seventh exercise of second chapter.}

\exercise{Eighth exercise of second chapter.}

\exercise{Nineth exercise of second chapter.}

\exercise{Tenth exercise of second chapter.}

\end{document}

在此处输入图片描述 在此处输入图片描述

相关内容