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

Kendall
  • 215

2 Answers2

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

frougon
  • 24,283
  • 1
  • 32
  • 55
  • 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 '20 at 19:16
  • 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 '20 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 '20 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 '20 at 19:53
  • Glad to help. In my above comment, I mixed up the order of the parameters (start and end markers are #2 and #3, text is #1), but I guess you got it. Just wanted to add, in case you didn't know, that since you are using cleveref, you can use multiple references like \Cref{c:1,c:3}, which would print “Cases (1) and (3)” in our example. This is a nice feature. – frougon Mar 17 '20 at 23:35
  • That's exactly why I wanted to use cleveref it makes documents neater and more organised from a tex file point of view. I did get that mixup when I played around with the parameters. But After finding that macro page I am getting to understand the mechanisms more clearly. – Kendall Mar 18 '20 at 13:32
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

murray
  • 7,944
  • 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 '20 at 19:24