2

I want to make a subnumcases-like environment, named mynumcases, like this

enter image description here

I will explain what I want and what I have tried.

  • what I want:
  1. equations in mynumcases have a parent counter which is same as equation
  2. I want to put this parent counter before the big brace, and has a fixed length away from left margin (I don't know what the left line's name), this length doesn't need to set as an argument.

enter image description here

  1. \Roman parent counter and \alph child counter
  2. (option) I can make my own tag at the position of the parent counter

Here is my code:

\documentclass{article}

\usepackage{amsmath, environ}
\usepackage{empheq}
\usepackage[showframe]{geometry}
\makeatletter
\def\Rom{\@Roman}

\NewEnviron{mynumcases}[2][\@nil]{%
\def\tmp@ne{#1}
\refstepcounter{equation}
\edef\oldeqnum{\theequation}
\setcounter{equation}{0}
\renewcommand{\theequation}{\alph{equation}}
\begin{empheq}[left={\makebox[0pt][r]{\makebox[.3\linewidth][l]{\ifx\tmp@ne\@nnil (\Rom{\oldeqnum}) \else #1 \fi}}#2\empheqlbrace}]{align}
  \BODY
\end{empheq}
\setcounter{equation}{\oldeqnum}
}
\makeatother

\begin{document}

first equation
\begin{equation}
  1 + 1 = 2
\end{equation}

first mynumcases

\begin{mynumcases}{}
  & 1 + 1 = 2\\
  & 1 = 1
\end{mynumcases}

another equation

\begin{equation}
  test
\end{equation}

second mynumcases

\begin{mynumcases}{f(x)=}
  & 1+1+1+1+1 & \text{if } hello\\
  & 1 + 1 & \text{if } hello\\
  & 1 & \text{if } hello\\
  & 1
\end{mynumcases}

third mynumcases

\begin{mynumcases}[my words]{f(x)=}
  & 1+1+1+1+1\\
  & 1 + 1\\
  & 1
\end{mynumcases}
\end{document}

and the whole output

enter image description here

And we can notice that

  1. the parent counter has the fixed length away from the equation, not left margin
  2. I have to put & before every line to make it align left
  3. if hello is far away from 1+1..

How can I reach my demand? Does anyone have ideas?

1 Answer 1

2

Here is the process I followed to achieve the requested solution:

  1. Store the coordinate of the left margin using eso-pic and zref's savepos module. We place a "label" lm at the start of the first page in the lower left corner of the text block. We'll use this x-coordinate in a calculation when setting any labels for mynumcases.

  2. Insert the appropriate equation label (either the optional argument or the actual equation number in \Roman) via \inserteqnum, by...

  3. ...inserting an appropriately-sized box to the left of empheq based on the location of the argument passed to empheq. The calculations use an equation-specific x-coordinate marker (also thanks to zref's savepos module), the aforementioned left margin x-coordinate and a predefined \empheqnumindent length that sets it off from the left margin.

enter image description here

\documentclass{article}

\usepackage{amsmath,zref-savepos}
\usepackage{empheq,eso-pic}
\usepackage[showframe]{geometry}

\AddToShipoutPictureBG*{\AtTextLowerLeft{\zsaveposx{lm}}}% Save left margin x-coordinate

\newlength{\empheqnumindent}
\setlength{\empheqnumindent}{25pt}
\newcounter{oldeqnum}
\newcommand{\inserteqnum}{%
  \zsaveposx{empheq-\theRequation}% Store position of LHS of empheq
  \makebox[0pt][r]{% Right-aligned 0pt-width box (pushes empheq number to the left)
    \makebox[\dimexpr\zposx{empheq-\theRequation}sp-\zposx{lm}sp-\empheqnumindent][l]{% Position equation number
      \theRequation% Set equation number
    }}%
}

\NewDocumentEnvironment{mynumcases}{o m}{%
  \IfValueF{#1}{\refstepcounter{equation}}% Only step equation counter if no optional argument
  % Store equation number (or optional argument)
  \begingroup\edef\x{\endgroup\gdef\noexpand\theRequation{%
    \IfValueTF{#1}
      {#1}
      {(\Roman{equation})}}}\x% Store equation number
  \setcounter{oldeqnum}{\value{equation}}% Store value of equation counter
  \setcounter{equation}{0}% Reset equation counter
  \renewcommand{\theequation}{\alph{equation}}% Update equation counter representation
  \empheq[left={\inserteqnum#2\empheqlbrace}]{align}
}{
  \endempheq
  \setcounter{equation}{\value{oldeqnum}}% Restore equation counter
}
\makeatother

\begin{document}

first equation
\begin{equation}
  1 + 1 = 2
\end{equation}

first mynumcases
\begin{mynumcases}{}
  & 1 + 1 = 2\\
  & 1 = 1
\end{mynumcases}
another equation
\begin{equation}
  test
\end{equation}

second mynumcases
\begin{mynumcases}{f(x)=}
  & 1 + 1 + 1 + 1 + 1 && \text{if } x   \\
  & 1 + 1             && \text{if } yy  \\
  & 1                 && \text{if } zzz \\
  & 1
\end{mynumcases}

third mynumcases
\begin{mynumcases}[my words]{f(x)=}
  & 1 + 1 + 1 + 1 + 1 \\
  & 1 + 1             \\
  & 1
\end{mynumcases}

\end{document}

Since this approach uses (La)TeX's \label-\ref-like system, any change in the page dimensions (technically, the left margin on the first page) or the mynumcases width would require an additional compilation.

This approach can be adapted for twoside more, or arbitrary page layouts. The approach would be to insert an page-specific left margin marker rather than just one at the start of the 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.