0

I have some code that looks like

\section*{Problem 1}

\begin{enumerate}
   \item Hello
   \item \label{problem-2b} Yes
   \item \cref{problem-2b} Welp
\end{enumerate}

I want the reference to appear as

problem 1a) 

as a refference. How can I include the name of the previous section into the reference? I already have the exercise number as a counter.

For a "MWE" of my actual problem see below

\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}
2
  • 2
    Why are you using \section*? Change the section format to use "Problem" so that you have access to the regular section counter. – Alan Munn May 26 '19 at 15:53
  • Your first code example is rather incomplete while your second one is not very minimal. This is why I basically just answered the question title. Could you make a true MWE that will be helpful to others finding this question? – schtandard Aug 3 '19 at 11:34
1

You can do that by redefining \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}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.