我有一些代码看起来像
\section*{Problem 1}
\begin{enumerate}
\item Hello
\item \label{problem-2b} Yes
\item \cref{problem-2b} Welp
\end{enumerate}
我希望引用显示为
problem 1a)
作为参考。我怎样才能将前一节的名称纳入参考文献中?我已经有锻炼号码作为计数器了。
有关我的实际问题的“MWE”请参见下文
\documentclass{article}
\usepackage{xparse}
\usepackage{etoolbox} %Neccecary for ifthenelse
\usepackage{enumitem}
\newcounter{tittel}
\setcounter{tittel}{0}
\newcounter{prob}[tittel]
\setcounter{prob}{0}
\newcounter{alternative}[prob]
\setcounter{alternative}{0}
\newcounter{alfa}[prob]
\newif\iffirstalt
\NewDocumentCommand{\Oppgave}{s o}{ %
\IfBooleanTF{#1}%
{\refstepcounter{alternative}\iffirstalt\else\refstepcounter{prob}\firstalttrue\fi}%
{\setcounter{alternative}{0}\refstepcounter{prob}\firstaltfalse}%
\section*{Oppgave~\arabic{prob}~%
\label{\arabic{tittel}.\arabic{prob}}
{\normalfont\IfBooleanTF{#1}{Alternativ \Roman{alternative}~}{}}%
\IfNoValueTF{#2}{}{\normalfont\normalsize (#2~poeng)}}%
\addcontentsline{toc}{section}{Oppgave~\arabic{prob}} \refstepcounter{alfa}
}
\makeatletter
\let\org@item\@item
\def\myspecial@item[#1]{%
\org@item[#1]%
\ifdef\@listctr
{%
\expandafter\ifdefequal\@listctr\@enumctr
{\addcontentsline{toc}{subsection}{\hspace{0.8em}\textbf{\alph{alfa})}}
\label{\arabic{tittel}.\arabic{prob}.\alph{alfa}}
\refstepcounter{alfa}}%
{}%
}{}%
}
\newenvironment{oppgaver}{%
\begin{enumerate}[ref={\alph{alfa}},topsep =1.2em,label=\textbf{\alph{alfa})},labelsep=1.5em,itemsep=0.7cm]
\let\@item\myspecial@item
}{%
\end{enumerate}
}
\makeatother
\usepackage{hyperref}
\hypersetup{
linktoc=all, % Link all the things in the toc
colorlinks=true, % false: boxed links; true: colored links
linkcolor=black, % color of internal links (change box color with linkbordercolor)
}
\usepackage[norsk, nameinlink, noabbrev]{cleveref}
\crefname{alfa}{oppgave}{oppgave}
\Crefname{alfa}{Oppgave}{Oppgave}
\begin{document}
\tableofcontents
\stepcounter{tittel}
\Oppgave[4] % Oppgave 1
\begin{oppgaver}
\item Prob 1
\item \label{prob1b} Prob 2
\item \cref{prob1b} Prob 3
\end{oppgaver}
\end{document}
答案1
您可以通过重新定义来做到这一点\p@enumi
。
\documentclass{article}
\makeatletter
\renewcommand*\p@enumi{\thesection.}
\makeatother
\begin{document}
\section{Some Section}
\begin{enumerate}
\item Hello
\item \label{some-item} Yes
\item Bye
\end{enumerate}
Look at item~\ref{some-item}
\end{document}