4

I just discovered the environment numcases, which upgrades the package cases allowing to number the different cases.

I am also currently using the showlabels package, which puts the name of the label on the PDF, right where you put it in the TeX file. Very useful feature, especially when writing a long document and you want to get the tag name from the PDF scrolling up a page rather than open another TeX file and search for the equation!

Unfortunately, I can't get the numcases environment to work properly with the showlabels package. In particular, I cannot give a label to the last case, otherwise I get the error "Incompatible list can't be unboxed". However, without the showcases package, everything works smoothly. It also works smoothly if I don't label the last case, but of course that's not optimal, cause I need to label all the cases...

Here's an example. Comment/uncomment the line where the showlabels package is included to see the behavior.

\documentclass{article}

\usepackage{showlabels}
\usepackage{cases}

\begin{document}

\begin{numcases}{}
a & b \label{a}\\
c & d \label{b}
\end{numcases}

\end{document}

Does anybody know a workaround for this? I would be fine also with a way to avoid numcases (while getting the same result).

2
3

I cannot understand why lines of a cases environment should be separately numbered. However, you get the effect with empheq:

\documentclass{article}

\usepackage{empheq}
\usepackage{showlabels}

\begin{document}

\begin{empheq}[left=\empheqlbrace]{alignat=2}
&a\qquad & b \label{a}\\
&c\qquad & d \label{b}
\end{empheq}

\end{document}

enter image description here

6
  • 1
    Well, when you have a system of equations and you want to refer to one of those, it's more elegant to have a number like 1.2 rather than "the second equation in 1". And yes, align does that, but I also like the braket in front. =) – bartgol Nov 12 '14 at 21:34
  • @bartgol I disagree. But the document is yours. ;-) – egreg Nov 12 '14 at 21:35
  • Well, if the system has quite a few equations and you want to refer to one in particular, it is more effective to say 1.5 rather than ask the reader to count the equations. Perhaps that's just me. As you said, to each its own. ;-) – bartgol Nov 12 '14 at 21:45
  • While this apparently worked in 2014, it now seems to produces two pairs of partially overlapping labels. – Circumscribe Nov 20 '18 at 18:38
  • I blame empheq, of course. :-) – egreg Nov 20 '18 at 18:46
4

One possibility is to use showkeys instead of showlabels (we've had healthy rivalry for a few decades:-) but if you want to stick with the opposition you can add some defensive coding to numcases so that the additional boxes don't break it:

enter image description here

\documentclass[a4paper]{article}

%\usepackage{showkeys}
\usepackage{showlabels}
\usepackage{cases}

\makeatletter
\renewenvironment{numcases}[1]%
{$$\numc@opts \setbox\z@\hbox
  {\advance\c@equation\@ne\def\@currentlabel{\p@equation\theequation}% local
  $\displaystyle {#1\null}\m@th$}%
 \numc@setsub 
 \setbox\tw@\vbox\bgroup
  \stepcounter{equation}\def\@currentlabel{\p@equation\theequation}% 
  \global\@eqnswtrue\m@th \everycr{}\tabskip\numc@left\let\\\@eqncr
  \halign to\dimen@ii \bgroup \kern\wd\z@ \kern14\p@ % assume width of brace
    \tabskip\z@skip \global\@eqcnt\@ne $\displaystyle{##}$\hfil 
   &\global\@eqcnt\tw@ \skip@10\p@ \advance\skip@\tw@\arraycolsep \hskip\skip@
    ##\unskip\hfil\tabskip\@centering% \unskip removes space if no explanations
   &\global\@eqcnt\thr@@\hbox to\z@\bgroup\hss##\egroup\tabskip\z@skip\cr
}{\@@eqncr \egroup % end \halign, which does not contain first column or brace
 \global\advance\c@equation\m@ne 
%Measure the natural width of the alignment
 \unskip\unpenalty\unskip\unpenalty \setbox\z@\lastbox % grab last line
%%%%% NEW TEST
\ifvbox\z@
\setbox\tw@\box\z@
\setbox\z@\lastbox
 \nointerlineskip \copy\z@ % then put it back
 \nointerlineskip \copy\tw@ % then put it back
\else
 \nointerlineskip \copy\z@ % then put it back
\fi
%%%%% END NEW
 \global\dimen@i\wd\z@ 
 \setbox\z@\hbox{\hskip-\numc@left\unhbox\z@}% Measure its natural width
 \ifdim \wd\z@<\dimen@i \global\dimen@i\wd\z@ \fi
\egroup% end \vbox (box\tw@, box\z@ is restored to LHS)
\hbox to\dimen@ii{\m@th % assemble the whole equation
  \hskip\numc@left 
  \hbox to\dimen@i{$\displaystyle \box\z@ % parameter #1
    \dimen@\ht\tw@ \advance\dimen@\dp\tw@ % get size of brace
    \left\{\vcenter to\dimen@{\vfil}\right.\n@space % make brace
    $\hfil}\hskip\@centering % finished first part (filled whole line)
  \kern-\dimen@ii % backspace the full width
  $\vcenter{\box\tw@}$% overlay the alignment
 }% end the \hbox to\dimen@ii
\numc@resetsub
$$\global\@ignoretrue}
\makeatother

\begin{document}

\begin{numcases}{}
a & b {\label{a}}\\
c & d {\label{b}}
\end{numcases}

\end{document}
5
  • Interesting option the showkeys. I think I will go with the empheq option proposed by egreg, just cause I was not super fond of numcases to strt with (I had to throw in dollar signs to insert math expressions). Just out of curiosity, what's the up/down side of using showkeys vs showlabels? They look pretty much the same to me at first look... – bartgol Nov 12 '14 at 21:41
  • @bartgol they are similar but patch in the output in different ways, both can break things, showkeys got lucky in this case (actually I think it is generally more robust, but then, I would) shame you gave the points to egreg though: he has far too many already:-) – David Carlisle Nov 12 '14 at 21:46
  • LoL: you guys are both in the 100k range. No need for change... ;-) Thanks for your feedback though. I will definitely consider showkeys in the future. =) – bartgol Nov 12 '14 at 21:48
  • @DavidCarlisle hrmmmm, I've made this slightly more robust, I think, in 1.7b1 – the rivalry continues! – Norman Gray Dec 6 '14 at 22:01
  • @NormanGray :-) – David Carlisle Dec 6 '14 at 22:17

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.