My MWE using LuaLaTeX:
\documentclass[oneside,DIV=12]{scrbook}
\usepackage{scrhack}
\usepackage[automark]{scrlayer-scrpage}
\usepackage[english]{babel}
\usepackage[babel]{microtype}
\usepackage{mathtools, amssymb}
\usepackage[warnings-off={mathtools-colon,mathtools-overbracket}]{unicode-math} % Math fonts
\setmathfont{Latin Modern Math}
\usepackage{setspace}\setdisplayskipstretch{}
\usepackage{enumitem}
\usepackage{xparse}
\usepackage{environ}
\usepackage{keyval}
\usepackage{lipsum}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% \adjintertext custom spacing \intertext macro (https://tex.stackexchange.com/a/280847/228055)
\MHInternalSyntaxOn
\newcommand{\adjintertext}[3]% #1=above skip, #2=below skip, #3=text
{\ifvmode\else\\\@empty\fi
\noalign{%
%\penalty\postdisplaypenalty\vskip\belowdisplayskip
\vskip-\lineskiplimit % CCS
\vskip\normallineskiplimit % CCS
\vskip#1
\vbox{\normalbaselines
\ifdim
\ifdim\@totalleftmargin=\z@
\linewidth
\else
-\maxdimen
\fi
=\columnwidth
\else \parshape\@ne \@totalleftmargin \linewidth
\fi
\noindent#3\par}%
%\penalty\predisplaypenalty\vskip\abovedisplayskip%
\vskip-\lineskiplimit % CCS
\vskip\normallineskiplimit % CCS
\vskip#2
}}%
\MHInternalSyntaxOff
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{document}
\onehalfspacing\KOMAoptions{DIV=current}
\lipsum[1][1-6]
\begin{flalign*}
& \text{For any numbers \(a\)}
&& \tag{P'10} \\
%%%%% the part related to the question %%%%%
\adjintertext{0pt}{12pt}{\centering%
$\begin{alignedat}{2}
& \text{(i) } & a &= b, \\
& \text{(ii) } & a &< b, \\
& \text{(iii) } & b &< a.
\end{alignedat}$%
}
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
& \text{For any numbers \(a\), \(b\), and \(c\), if \(a < b\) and \(b < c\), then \(a < c\).}
&& \tag{P'11} \\
& \text{For any numbers \(a\), \(b\), and \(c\), if \(a < b\), then \(a + c < b + c\).}
&& \tag{P'12} \\
& \text{For any numbers \(a\), \(b\), and \(c\), if \(a < b\) abd \(0 < c\), then \(ac < bc\).}
&& \tag{P'13}
\end{flalign*}
\lipsum[2][1-8]
\end{document}
I wanna make a new environment myalignedat
to typeset centered and aligned equations like my MWE such that instead of typing
\begin{<display math mode environment>}
\adjintertext{0pt}{12pt}{\centering%
\(\begin{alignedat}{2}
& \text{(i) } & a &= b, \\
& \text{(ii) } & a &< b, \\
& \text{(iii) } & b &< a.
\end{alignedat}\)%
}
\end{<display math mode environment>}
I would type the following instead:
\begin{<display math mode environment>}
\begin{myalignedat}{2}[before skip=0pt, after skip=12pt]
& \text{(i) } & a &= b, \\
& \text{(ii) } & a &< b, \\
& \text{(iii) } & b &< a.
\end{myalignedat}
\end{<display math mode environment>}
similar to how the tcolorbox
does it for its environments.
My question is: How can I define such an environment?
I've tried using \newenviron
or the environ
and xparse
packages but to no avail since I have little knowledge about the matter. Thank you.
Best Answer
Original answer
For this to work we have to somehow get the
\noalign
at a position where TeX finds it (inside of the\begin{myalignedat}
definition seems too late for this). Therefore the following uses theenv/myalignedat/before
andenv/myalignedat/after
hooks along with some brace trick (\ifnum0=`{
and\ifnum0=`}
) to get the\noalign
to where we need it. Also we need to reimplement the\adjintertext
code in our environment.Once we have that out of our way the rest is pretty straight forward, we just collect an optional and a mandatory argument and use any
key=val
implementation that fits our needs (the following usesexpkv
with theexpkv-def
key-defining frontend1, but other packages would work as well). Another small adjustment (that one isn't really necessary though) is that the following doesn't use\begin{alignedat}...\end{alignedat}
but\alignedat...\endalignedat
instead.Complete code:
Output:
1Disclaimer: I'm the package author of
expkv
and friendsImproved version needing to hack
\begin
The above code version couldn't detect whether a
\\
was used before it or not, and thus lead to inconsistent vertical spacing. The following fixes that by patching LaTeX's\begin
to have another hook. The patch should be fine and not affect any other usage of\begin
.Pseudo-environment version
This version uses a fully expandable macro to start a pseudo-environment. It doesn't require any hacking of standard LaTeX macros, but it still uses the brace hacks. It expands to
\noalign
directly and can then use unexpandable argument grabbing and key=value parsing.The environment is started by
\beginmyalignedat[<key=value>]{<alignedat-arg>}
and ended by\stopmyalignedat
.Complete code:
Normal macro version
A macro variant that just grabs the arguments in an expandable way, uses an expandable key=value interface, and hence "directly" expands to
\adjintertext
, such that it behaves as if\adjintertext
was used like in your code. An alternative version could use the same brace trick like the other solutions to use non-expandable argument grabbing and key=value solutions.The syntax is:
Complete code: