1

I recently used Roelof's answer from list the cases in text and I would like to be able to use label Cref to call the different Cases.

The environment is

\newcounter{casenum}
\newenvironment{caseof}{\setcounter{casenum}{1}}{\vskip.5\baselineskip}
\newcommand{\case}[2]{\vskip.5\baselineskip\par\noindent {\bfseries Case \arabic{casenum}:} #1\\#2\addtocounter{casenum}{1}}

and the example edited is

\documentclass{article}
\begin{document}
  \newcounter{casenum}
  \newenvironment{caseof}{\setcounter{casenum}{1}}{\vskip.5\baselineskip}
  \newcommand{\case}[2]{\vskip.5\baselineskip\par\noindent {\bfseries Case \arabic{casenum}:} #1\\#2\addtocounter{casenum}{1}}
  \begin{caseof}
    \case{$x>0$}{
      In this case, $x$ is larger than $0$ \label{c:1}
    }
    \case{$x<0$}{
      In this case, $x$ is smaller than $0$ \label{c:2}
    }
    \case{$x=0$}{In this case, $x$ equals $0$\label{c:3}}
  \end{caseof}
\end{document}

How can I go about changing Cref so that I can use say

\Cref{c:1} is a good example for positive $x$

and have Latex output it as

Case (1) is a good example for positive x.

If I use \roman instead it should say

Case (i) is a good example for positive x.

etc

where, just as for normal equations, you can click on the (1)/(i) and it will direct you to that part of the document.

Many Thanks

2 Answers 2

2

If you really want bold references:

\documentclass{article}
\usepackage{hyperref}
\usepackage[nameinlink]{cleveref}

\newcounter{casenum}
\newenvironment{caseof}
  {\setcounter{casenum}{0}}
  {\par\addvspace{.5\baselineskip}}

\renewcommand*{\thecasenum}{\arabic{casenum}}

\newcommand{\case}[2]{%
  \par\addvspace{.5\baselineskip}%
  \noindent \refstepcounter{casenum}\textbf{Case \thecasenum:}~#1\\*
  #2\ifhmode\unskip\fi
}

\crefname{casenum}{\protect\textbf{case}}{\protect\textbf{cases}}
\Crefname{casenum}{\protect\textbf{Case}}{\protect\textbf{Cases}}
\creflabelformat{casenum}{#2\textbf{(#1)}#3}

\begin{document}

  \begin{caseof}
    \case{$x>0$\label{c:1}}{%
      In this case, $x$ is larger than $0$.%
    }
    \case{$x<0$\label{c:2}}{%
      In this case, $x$ is smaller than $0$.%
    }
    \case{$x=0$\label{c:3}}{%
      In this case, $x$ equals $0$.%
    }
  \end{caseof}

  \Cref{c:1} is a good example for positive $x$.

\end{document}

enter image description here

Note that I used \\* to prevent a page break, and you might want to reimplement your caseof environment with enumitem (which would be another question).

If you don't want bold references, replace:

\crefname{casenum}{\protect\textbf{case}}{\protect\textbf{cases}}
\Crefname{casenum}{\protect\textbf{Case}}{\protect\textbf{Cases}}
\creflabelformat{casenum}{#2\textbf{(#1)}#3}

with:

\crefname{casenum}{case}{cases}
\creflabelformat{casenum}{#2(#1)#3}

enter image description here

For roman numbering, replace:

\renewcommand*{\thecasenum}{\arabic{casenum}}

with:

\renewcommand*{\thecasenum}{\roman{casenum}}

enter image description here

7
  • Thank you so much! The bold/roman numerals were just examples, I am used to changing those around. The issue I have is when trying to add options to inbuilt commands. I am assuming the \Crefname and \Creflabelformat are the one I need to change for different options where the #1,#2,#3 stand for the options inside brackets \Crefname{#1}{#2}{#3} ?
    – Kendall
    Mar 17, 2020 at 19:16
  • I also stumbled across this <tex.stackexchange.com/questions/172487/…> which I think is going to answer most of those questions.
    – Kendall
    Mar 17, 2020 at 19:19
  • Regarding what you said, answers are in the cleveref manual, but in short: #1 and #3 in the argument of \creflabelformat are start and end markers for the hyperlinks, and #2 is the actual text. I made little changes but can't test them right now, as I am commenting using a smartphone.
    – frougon
    Mar 17, 2020 at 19:31
  • Also, you could take the label from an optional argument defaulting to empty (the label would then be #1; add it from the \case macro code with etoolbox: \ifstrempty{#1}{}{\label{#1}}). This has to come after the \refstepcounter.
    – frougon
    Mar 17, 2020 at 19:47
  • I have been reading the manual and I have managed to fine tune it perfectly. Thank you so much for the help.
    – Kendall
    Mar 17, 2020 at 19:53
1

You could also use the enumitem package, as follows.

\documentclass{memoir}
\usepackage{hyperref}
\usepackage[nameinlink]{cleveref}

\usepackage{enumitem}

\newlist{romcases}{enumerate}{3}
\setlist[romcases,1]{%
    label=\sffamily{Case (\roman*):},
    ref=\normalfont{(\roman*)},
    wide, labelindent=0pt,itemsep=0pt,topsep=0pt
}
\crefname{romcasesi}{Case}{Cases}
\Crefname{romcasesi}{Case}{Cases}

\begin{document}

\begin{romcases}

  \item $x>0$\label{c:1}\\
    In this case, $x$ is larger than $0$.

  \item $x<0$\label{c:2}\\
    In this case, $x$ is smaller than $0$.

  \item $x=0$\label{c:3}\\
    In this case, $x$ equals $0$.

\end{romcases}

\Cref{c:1} is a good example for positive $x$.

\end{document}

Cases list using enumitem

1
  • This makes life so much simpler. Thank you for this answer as well. I really need to get used to altering macros so I am going to play around with the settings in frougons answer. This is definitely something I'll keep in mind for future and will be of use to anyone else who comes to this question.
    – Kendall
    Mar 17, 2020 at 19:24

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.