Skip to content

Commit

Permalink
Replace chemformula with chemmacros
Browse files Browse the repository at this point in the history
Many more features, especially a predefined reaction environment
  • Loading branch information
alexpovel committed Oct 9, 2021
1 parent c584129 commit e379fa9
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 45 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ dirty_directory/
*.aux
*.lof
*.log
# List of Reactions (`chemmacros`):
*.lor
*.lot
*.fls
*.out
Expand Down
2 changes: 2 additions & 0 deletions chapters/frontmatter.tex
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,6 @@

\listoflistings%

\listofreactions%

\subimport{frontmatter/}{preface}
50 changes: 31 additions & 19 deletions chapters/mainmatter/base-features.tex
Original file line number Diff line number Diff line change
Expand Up @@ -494,11 +494,27 @@ \subsubsection{Physical Units}
\subsubsection{Chemistry}

Closely related to purely mathematical equations are chemical ones,
as shown in \cref{reac:meaningless}.
\begin{chemreac}\label{reac:meaningless}
\ch{C + O2 -> CO2}
\end{chemreac}
This functionality is basic, but easily edited and built upon.
as shown in \cref{reac:carbon-oxidation}.
% As with the `equations` environment, the starred environment variant omits
% numbering.
\begin{reaction}[%
Carbon~Oxidation% This is a description/caption going into the list of reactions
]
% Quotes around `label` are important for escaping, see
% https://tex.stackexchange.com/q/596722/120853
C + O2 -> CO2 "\label{reac:carbon-oxidation}"
\end{reaction}
Aligned arrays of multiple chemical reaction equations are also possible, as
demonstrated in \cref{reac:nitrogen-oxidation-one,reac:nitrogen-oxidation-two}.
\begin{reactions}
% For usage of `\sb`, see https://tex.stackexchange.com/a/48818/120853 and
% https://tex.stackexchange.com/a/271498/120853 .
% In an array of multiple reactions, we need \AddRxnDesc, see the docs.
N2 + O &<->[\(k\sb{1}\)] NO + N
\AddRxnDesc{First~Zeldovich~mechanism} "\label{reac:nitrogen-oxidation-one}" \\
N + O2 &<->[\(k\sb{2}\)] NO + O
\AddRxnDesc{Second~Zeldovich~mechanism} "\label{reac:nitrogen-oxidation-two}"
\end{reactions}
Single chemical compounds are invoked using \verb|\chcpd|, as demonstrated in
\cref{eq:boie}.
\begin{equation}\label{eq:boie}
Expand All @@ -523,21 +539,17 @@ \subsubsection{Chemistry}
}
\end{equation}

These commands are provided by \ctanpackage{chemformula}, a stand\-/alone package
that heavily builds on \ctanpackage{chemmacros} of the same author.
These commands are provided by \ctanpackage{chemmacros} and its \ctanpackage{chemformula}
module.
Those packages are very capable, so more complex chemistry can also be typeset:
\begin{gather*}
\ch{^{227}_{90}Th+}\\
\ch{[Cu(NH3)4]^2+}\\
\ch{RNO2^{-.}}\\
\ch{CH+CH}\\
\ch{\{[CH2=CH-CH2]- <-> {}[CH2-CH=CH2]- \}}\\
\ch{
H2C-C+C-CH=CH+ + CrO4^2-
<=>[x][y]
2.5 Cl^{-.} + 3_1/2 Na*OH_{(aq)} + !(name)( A^n )
}
\end{gather*}
\begin{reactionsgather*}
^{227}_{90}Th+\\
[Cu(NH3)4]^2+\\
RNO2^{-.}\\
CH+CH\\
\{[CH2=CH-CH2]- <-> {}[CH2-CH=CH2]- \}\\
H2C-C+C-CH=CH+ + CrO4^2- <=>[x][y] 2.5 Cl^{-.} + 3_1/2 Na*OH_{(aq)} + !(name)( A^n )
\end{reactionsgather*}
All these examples are courtesy of the documentations of these two packages.

\subsection{Sans-Serif}
Expand Down
40 changes: 14 additions & 26 deletions docker/tex/acp.cls
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@
mathtt=sym,%
% 'tpgl': 'TexGyrePagellaLining'.
% This 'NFSS' style is required for \fontfamily{nfss-code}\selectfont to work,
% e.g. in package chemformula
% e.g. in package chemmacros/chemformula
NFSSFamily=tgpl,%
% Color=Green4,% Toggle color for debugging (should be xcolor name)
]{TeX Gyre Pagella Math}% Load existing fond, see also https://tex.stackexchange.com/a/351100/120853
Expand Down Expand Up @@ -2529,29 +2529,21 @@
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\RequirePackage{chemformula}% \ch{}, \chcpd[]{compound} etc.
\setchemformula{%
font-family={tgpl}% Use math font family
\RequirePackage{chemmacros}% \ch{}, \chcpd[]{compound} etc.
\usechemmodule{all}% `all` includes `chemformula`
\chemsetup{%
formula=chemformula,%
chemformula/font-family=tgpl,% Use math font family
}

% Provide a new environment for chemical reactions.
% I tried the syntax from the 'environ' package and also the \NewDocumentEnvironment
% command from 'xparse', but none of them worked especially well, so it's back to
% plain syntax, unfortunately.
\newcounter{chemreac}[chapter]
%
\newenvironment{chemreac}{%
\addtocounter{equation}{-1}
\renewcommand*{\theequation}{\textbf{R}\thechapter.\thechemreac}
\begin{equation}
% Our counter is called 'chemreac', however it seems that cref always uses
% the 'type' of the last \refstepcounter{} called.
% Therefore, step our custom counter after equation starts
% see https://tex.stackexchange.com/a/239121/120853
\refstepcounter{chemreac}
}{%
\end{equation}%
}%
% Some new custom environments. Very simple definitions that behave just like the
% widely known AMSMath math environment as specified in the last argument.
\NewChemReaction{reactionsgather}{gather}% Like AMSmath's `gather`
\NewChemReaction{reactionsgather*}{gather*}

% Sort reaction counter underneath chapter, see
% https://tex.stackexchange.com/questions/530210/how-to-cross-reference-reactions-in-chemmacros-chapter#comment1340793_530210
\renewcommand*{\thereaction}{\thechapter.\arabic{reaction}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Expand Down Expand Up @@ -3150,10 +3142,6 @@
% \Crefname (capitalised) is not required; when it is not present but \Cref{} is
% called, it uses \MakeUppercase on \crefname definitions.
% However, if LaTeX macros are used in the definition, it is required.
\crefname{chemreac}{\TransReaction{}}{\TransReactions{}}
\Crefname{chemreac}{\TransReaction{}}{\TransReactions{}}
\creflabelformat{chemreac}{#2\textbf{R}#1#3}% No (1), but 1, aka remove parentheses
% Code listings, see `minted` package.
% Introduce a new <type> called `code`, then make the (existing) counter `listing`
% use it by aliasing. Modifying `listing` directly with `\crefname{listing}...`
Expand Down

0 comments on commit e379fa9

Please sign in to comment.