5

I'm writing a large thesis, and recently discovered the autonum package, which will automatically only number equations which are referenced in the text. This seems like a very good addition to my preamble, but unfortunately including autonum will undefine all the starred amsmath environments (mostly affects align* in my case), and so a document which uses any of the starred environments won't compile.

Now I'm not 100% certain that I want to commit to using the autonum package just yet, so instead of replacing all instances of align* with align (which is hard to undo down the road), I wanted to simply redefine align* environment to do the same thing as align does.

My attempts so far have been

\newenvironment{align*}
{\begin{align}}{\end{align}}

which fails with the error Command \align* already defined. So I tried \renew...

\renewenvironment{align*}
{\begin{align}}{\end{align}}

but that fails with the error Environment align* undefined.

Minimal working (or non-working if you want) example

\documentclass{article}
\usepackage{amsmath}
\usepackage{cleveref}
\usepackage{autonum}

% this gives the errors
%   Command \align* already defined. {\begin{align}}{\end{align}}
%   Environment align* undefined. \begin{align*}
% \newenvironment{align*}
% {\begin{align}}{\end{align}}

% this gives the error
%   Environment align* undefined. \begin{align*}
% \renewenvironment{align*}
% {\begin{align}}{\end{align}}

\begin{document}
\begin{align} % labelled and referenced equation -- will get numbered
    a = b + c
    \label{eq:eq1}
\end{align}
Refer to equation \cref{eq:eq1}.
\begin{align} % labelled equation -- will not get numbered
    a = b + c
    \label{eq:eq2}
\end{align}
\begin{align*} % starred envir, will crash compilation
    a = b + c
\end{align*}
\end{document}
  • 1
    I think that the autonum package should have an option for not disabling the starred environments. This could be a feature request to the author. – egreg Apr 13 '16 at 13:32
  • \renewenvironment{align*}{\align\color{red}}{\endalign} – user66081 May 12 '16 at 2:00
5

You can do all starred environments by doing the appropriate definitions at begin document:

\documentclass{article}
\usepackage{amsmath}
\usepackage{cleveref}
\usepackage{autonum}

\makeatletter
\newcommand{\restore@Environment}[1]{%
  \AtBeginDocument{%
    \csletcs{#1*}{#1}%
    \csletcs{end#1*}{end#1}%
  }%
}
\forcsvlist\restore@Environment{alignat,equation,gather,multline,flalign,align}
\makeatother

\begin{document}

\begin{align} % labelled and referenced equation -- will get numbered
    a = b + c
    \label{eq:eq1}
\end{align}
Refer to equation \cref{eq:eq1}.
\begin{align} % labelled equation -- will not get numbered
    a = b + c
    \label{eq:eq2}
\end{align}
\begin{align*}
    a = b + c
\end{align*}
\end{document}

In my experiment, using \tag will add the tag independently of whether the tagged equation is referenced.

enter image description here

| improve this answer | |
  • @Patrick Häcker Do you think it would be possible to implement that into your package? – jpmath Dec 12 '17 at 10:00
2

I don’t know if I understand correctly what you are asking for, but see if this works for you:

\documentclass{article}
\usepackage{amsmath}
\usepackage{cleveref}
\usepackage{autonum}

% this gives the errors
%   Command \align* already defined. {\begin{align}}{\end{align}}
%   Environment align* undefined. \begin{align*}
% \newenvironment{align*}
% {\begin{align}}{\end{align}}

% this gives the error
%   Environment align* undefined. \begin{align*}
% \renewenvironment{align*}
% {\begin{align}}{\end{align}}

\begin{document}

\makeatletter

\@namedef{align*}{%
  \start@align\@ne\st@rredfalse\m@ne
}
\@namedef{endalign*}{\endalign}

\makeatother

Some text.
\begin{align} % labelled and referenced equation -- will get numbered
    a = b + c
    \label{eq:eq1}
\end{align}
Some text.  Refer to equation \cref{eq:eq1}.
\begin{align} % labelled equation -- will not get numbered
    a = b + c
    \label{eq:eq2}
\end{align}
Some text.
\begin{align*} % starred envir, will crash compilation
    a = b + c
\end{align*}
\end{document}

Note that the patch to the commands must be inserted after \begin{document}.

| improve this answer | |
  • A very good solution, but I prefer egreg's solution. Also, he was first :) – Filip S. Apr 14 '16 at 6:22
  • @FilipS.: I think I answered first, but I too prefer egreg’s solution over mine: it is more elegant. – GuM Apr 14 '16 at 10:34
  • Ah, sorry, I must have misread the timestamps. – Filip S. Apr 15 '16 at 8:30
1

If you are free to use LuaLaTeX, adding the following seven lines of code to your preamble will automatically replace all instances of {align*} with {align} "on the fly", before TeX's "eyes" start their processing. This way, LaTeX will never "see" align* environments, as all of them will have been converted to align environments.

\usepackage{luacode}
\begin{luacode}
function alignstar ( buff )
   return ( string.gsub ( buff, "{align%*}", "{align}" ) )
end
luatexbase.add_to_callback ( "process_input_buffer", alignstar, "alignstar" )
\end{luacode}

A full MWE:

enter image description here

% !TEX TS-program = lualatex
\documentclass{article}
\usepackage{amsmath,cleveref,autonum}

\usepackage{luacode}
\begin{luacode}
function alignstar ( buff )
   return ( string.gsub ( buff, "{align%*}", "{align}" ) )
end
luatexbase.add_to_callback ( "process_input_buffer", alignstar, "alignstar" )
\end{luacode}

\begin{document}
\begin{align} % labelled and referenced equation -- will get numbered
    a = b + c
    \label{eq:eq1}
\end{align}
Refer to equation \cref{eq:eq1}.
\begin{align} % labelled equation -- will not get numbered
    a = b + c
    \label{eq:eq2}
\end{align}
\begin{align*} % starred envir, will no longer crash compilation
    a = b + c
\end{align*}
\end{document}

Addendum: If you need to fix not only all align* environments but all gather* environments as well, all you'd need to do is replace the line

   return ( string.gsub ( buff, "{align%*}", "{align}" ) )

with

   buff = string.gsub ( buff, "{align%*}", "{align}" ) 
   buff = string.gsub ( buff, "{gather%*}", "{gather}" ) 
   return buff
| improve this answer | |
  • A very slick solution, and how I envisioned solving it, but I think I prefer pdfLaTeX for now. – Filip S. Apr 14 '16 at 6:25
  • @FilipS. - Glad you liked my solution. :-) Just out of idle curiosity: Is there something about LuaLaTeX that's giving you pause or is otherwise deterring you from using it? – Mico Apr 14 '16 at 6:32
  • Mostly just that 1) I don't know Lua 2) it seems kind of overkill for this problem considering egreg's nice LaTeX-solution, and 3) I would have to reconfigure my build setup (I'm using Sublime Text 3 + LaTeXing on Windows 10 to compile). – Filip S. Apr 14 '16 at 7:42

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.