14

I would like to define

$$$ \zeta(s) = \dfrac1{1^s} + \dfrac1{2^s} + \cdots $$$

as a short hand for

\begin{align}
\zeta(s) = \dfrac1{1^s} + \dfrac1{2^s} + \cdots
\end{align}`

How should I go about doing this?

PS: I am aware of using newcommand to define something like

\newcommand{\ba}[1]{\begin{align}#1 \end{align}}
E.P.
  • 1,415
Adhvaitha
  • 307

6 Answers6

42

You can. But I strongly discourage you to use the following code that implements your idea. The resulting document code is obscure and error prone. If you forget a $ somewhere, you'll probably get weird error messages when TeX is very far from the point where the missing $ should be.

Disclaimer. Using this code can cause kittens die; it can also cause the computer to rebel against you and create a computer domination over the world. You've been advised.

\documentclass{article}
\usepackage{amsmath,array}

\let\normaldollar=$
\catcode`$=\active
\makeatletter
\protected\def${\new@ifnextchar${\check@two}{\(\close@single}}
\def\close@single#1${#1\)}
\def\check@two#1{\@ifstar{\do@equation@star}{\check@three}}
\def\check@three{\new@ifnextchar${\check@three@star}{\do@equation}}
\def\check@three@star#1{\@ifstar{\do@align@star}{\do@align}}
\def\do@equation@star#1$${\[#1\]}
\def\do@equation#1$${\begin{equation}#1\end{equation}}
\def\do@align#1$$${\begin{align}#1\end{align}}
\def\do@align@star#1$$${\begin{align*}#1\end{align*}}
\makeatother

\begin{document}
In line math $abc$, followed by a numbered equation
$$
1+1=\left\lbrace\begin{array}{@{}l>{\normaldollar}l<{\normaldollar}@{}}
2 & if it rains\\
3 & otherwise
\end{array}\right.
$$
followed by an unnumbered equation
$$*
2+2=4
$$
followed by a numbered align
$$$
a&=b\\
c&=d
$$$
followed by an unnumbered align
$$$*
f&=g\\
&=h
$$$
\end{document}

For example, forgetting the $ after abc, you get an error at line 34, which reads

! Misplaced alignment tab character &.

Try it. Then forget about this idea. Using \newcommand{\ba}[1]{\begin{align}#1\end{align}} is even worse.

enter image description here

egreg
  • 1,121,712
  • 31
    +1 for the disclaimer. i'd also warn against being anywhere near an active volcano or a plate boundary. – barbara beeton Dec 02 '14 at 21:13
  • This code will break, though, with $$$$ -- which is entirely OK in Plain-TeX, and legal but discouraged-because-deprecated in LaTeX. :-) – Mico Dec 02 '14 at 21:18
  • 5
    @Mico The code can go wrong in uncountably many ways. – egreg Dec 02 '14 at 21:20
  • 1
    I also love the macro \normaldollar: it'll certainly appeal to the OP's desire to save himself/herself from having to type a few extra keystrokes. :-) – Mico Dec 02 '14 at 21:24
  • 2
    Oh no! Kittens noooooooooo! – Paulo Cereda Dec 02 '14 at 21:27
  • 1
    Thanks. I decide what I want. \LaTeX should not dictate terms to me. – Adhvaitha Dec 02 '14 at 21:28
  • 6
    @Adhvaitha It's not LaTeX but people that have been using TeX for many many years. Many of them involved in professional publications. May be, just may be, you should try to understand their point. – Manuel Dec 02 '14 at 21:30
  • @Manuel I too publish articles using LaTeX. It is a drawback of LaTeX if we cannot define shorthand notations/ functions. The answers provided here might be not the best way, but any good language should provide adequate support for defining new functions. – Adhvaitha Dec 02 '14 at 21:32
  • 1
    @Adhvaitha You can save some minutes typing the document; most probably you spend hours in debugging it, if you choose to use shorthands like these. – egreg Dec 02 '14 at 21:33
  • @egreg What is wrong in defining functions like in C++ or any other programming language? In fact, using functions instead of repeatedly coding the same thing is strongly encouraged in all programming languages. – Adhvaitha Dec 02 '14 at 21:36
  • 7
    @Adhvaitha The function is already there, easy to use and much less obfuscated: \begin{align}...\end{align}. – egreg Dec 02 '14 at 21:37
  • @egreg But why should the language prevent me from defining my own function? – Adhvaitha Dec 02 '14 at 21:38
  • 3
    @Adhvaitha I showed you that the language doesn't prevent you from writing and using such obfuscated code. The more clumsy and unreadable shorthands you use, the less you'll be able to maintain the code. – egreg Dec 02 '14 at 21:40
  • 11
    @Adhvaitha --- I don't know much C++ but I wonder if you could define a function called {{{. If you can, would you do so? – Ian Thompson Dec 02 '14 at 21:41
  • 9
    @Adhvaitha -- as a long-time practitioner of tex, i have spent many hours debugging obscure code created by other people. from this and from them i have learned that the simpler and more obvious the code presentation (even if it's "longer" because it takes more keystrokes), the more (unpleasant) debugging time is avoided. if you have ever had to debug a tricky (la)tex problem, you will know that the available debugging tools are among the weakest part of the system. make things easy on yourself -- and others who have to decipher your input. – barbara beeton Dec 02 '14 at 21:46
  • @barbarabeeton Thanks for your suggestion but given that my document is going to 10-20 pages long, it seems a poor reasoning to me to avoid something just because debugging is going to be hard. What also baffles me is the following: if $$...$$ is not to be used and \[...\] has to be used, why is $$...$$ still made available? Why cannot $$...$$ be mapped to \[...\]? – Adhvaitha Dec 02 '14 at 21:48
  • 4
    @Adhvaitha - Please see the posting http://tex.stackexchange.com/a/40531/5001 for a comparison of $$ ... $$ and \[ ... \] methods. The latter method offers significant extra error checking. That's one solid reason for not using $$ ... $$ directly in a LaTeX-based document. – Mico Dec 02 '14 at 22:07
  • @Mico You are not answering my question: If $$...$$ is not to be used and \[...\] has to be used, why is $$...$$ still made available? Why cannot $$...$$ be mapped to \[...\]? – Adhvaitha Dec 02 '14 at 22:08
  • 4
    @Adhvaitha Nobody is saying that it is bad to define shorthands in general. But it is a very bad idea to define the particular shorthand you requested. $ is not a regular character as far as TeX is concerned. I assume there are similar characters in C++: would use use those to name a function? If you map $$ to \[, you would create a circular definition, I think. Take a look at the answer Mico pointed you to. – cfr Dec 02 '14 at 22:10
  • 6
    @Adhvaitha $$ can't be disabled, because it's a primitive feature of TeX, which works in a very different way than macros. Its meaning is fixed, not changeable as macros are. Of course, changing the category code of $ effectively disables it, but also disables being able to use $...$ for inline formulas. – egreg Dec 02 '14 at 22:16
  • 5
    @Adhvaitha -- i'm not the best person to answer your question about mapping. however, one pretty much has to read the "internal" latex coding to find the $$; it's not mentioned at all in lamport, and it's certainly not presented as a recommendation in any other good latex manual. it's impossible to ignore, since it's essentially a tex primitive (rather the physical manifestation of a primitive tex function), but is really better "forgotten". as for a 10-20 page document, as long as you're the only one ever to look at it, okay, but heaven help the editors if it goes to a journal. – barbara beeton Dec 02 '14 at 22:17
  • 1
    I second @barbarabeeton last wise remarks. Even if it is a document with only a few pages, you never know if that same document, might be requested later on, or included as reference material in other documents. And then you realize, for the sake of consistency in your code, is best to revert the mapping, since the ones that are "important" to you, do not have them... – doed Dec 03 '14 at 00:08
  • 5
    I am sitting here grinning, thinking of the poor bastard at any journal publishing a paper with this level of hack scattered throughout. The grinning is because I am (so far) not that poor bastard. – Brian B Dec 03 '14 at 03:15
  • 1
    @BrianB Only due to the very permissive policy of the journal I typeset, we don't instantly reject these sorts of crap. Many journals don't reject them because they don't quite "typeset" the articles; they sometimes even take the PDF from authors, delete the headers and footers and inject correct headers, footers and page numbers. [citation needed; I have this only from non-official sources] – yo' Dec 03 '14 at 09:41
  • 1
    @tohecz -- the ams used to accept "camera-ready" copy from authors for book proceedings, but the policy now is that everything must be tex'ed in-house (except for "edge" cases where a volume of collected works might reproduce papers published as far back as the 1920s; those might be included as scanned-to-pdf pages). and if a paper is received with code as described here, the pdf "verification copy" would be sent to a keyboarder for rekeying in the specified style. no guarantee there won't be new errors inserted, which the author will be held responsible for finding. – barbara beeton Dec 03 '14 at 13:21
  • @barbarabeeton I support that idea, based on my experience (well your experience is quite bigger than mine). And I agree that if errors are introduced in the process, it's up to the authors to find them. (Btw, I hope that my article accepted to AMS isn't a trouble-maker... :) ) – yo' Dec 03 '14 at 13:24
  • @tohecz -- if you followed "the rules", it won't be, particularly if you used the author package provided. (and if you tell me -- off-line -- what publication it appear(ed|s) in, i'll be happy to look it up and let you know.) – barbara beeton Dec 03 '14 at 13:27
  • 1
    @barbarabeeton: in the situation you describe, where the whole thing is re-coded because of bad LaTeXery, would the authors be informed? – Peter LeFanu Lumsdaine Dec 03 '14 at 18:14
  • @PeterLeFanuLumsdaine -- it depends. usually, the authors would be notified and offered the chance to redo the file(s) according to the published requirements, but if the schedule is really tight (as it is in the case of some proceedings volumes, and if the volume editor has not already notified the author), notification may not come until after the fact. it's the exception that the author won't be notified beforehand, but i can't promise that it never happens. – barbara beeton Dec 03 '14 at 18:30
  • @barbarabeeton: Thanks; that’s both interesting and reassuring to know. – Peter LeFanu Lumsdaine Dec 03 '14 at 18:30
17

You shouldn't. You actually shouldn't use $$...$$ either in LaTeX: Why is \[ … \] preferable to $$?

It is probably doable, but would be very difficult to do so that it doesn't break things like these two (\usepackage{array} needed for the 2nd one):

$$$ x_n = 0 \quad\text{for $n=1,2,3,\dotsm$} $$$
\begin{tabular}{>$l<$} 3x^2 \\ $Hello, World!$ \end{tabular}

Please, please, write a legible code and don't use cryptic shorthands like proposed $$$...$$$ or (maybe even worse) \ba{...}.


As giordano points out, if you want to type the code faster, ask a question on how to type the code faster, and not how to shorten the code. For typing the code faster, you can find some good editors that support various kinds of auto-completion: LaTeX Editors/IDEs

yo'
  • 51,322
10

If we are using plain TeX then our life is simpler. We are typing:

$$\eqalign{
   a &= b + c \cr
   c &= d + e
}$$

when we need to typeset aligned equations. And your idea about $$$ ... $$$ can be implemented simply by \everydisplay:

\everydisplay={\futurelet\next\eqalignQ}
\def\eqalignQ{\ifx\next$\expandafter\eqalignX\fi}
\def\eqalignX$#1$$${\eqalign{#1}$$}

$$$
   a &= b + c \cr
   c &= d + e
$$$

\end

And we have no LaTeX-syntactical purists in our own ranks.

wipet
  • 74,238
  • 1
    Of course, because this will not work with LaTeX and amsmath. Oh, and by the way, the question asked for align, so the equations should be numbered. – egreg Dec 03 '14 at 09:28
  • 2
    If the align environment is used only for equation numbers (no for aligning more equations) then this is IMHO very bad idea. Compare the vertical space between short paragraph followed by \begin{align}a=b\end{align} and the same paragraph followed by $$a=b \eqno (1)$$. What space is better? The second, because the \abovedisplayshortskip is used here. On the other hand the align environment generates the virtual equation of the full \displaywidth and only \abovedisplayskip is used before it. – wipet Dec 03 '14 at 16:05
6

The earlier answers have already provided several solutions, all the while warning you against pursuing your objective. Here's a LuaLaTeX-based solution that does what you want. Importantly, the earlier warnings do not apply to this solution, as it doesn't actually redefine $$$ in any way. Instead, it sets up a function that acts as a pre-processor that sweeps over all input lines before TeX starts any of its regular processing, converting all instances of $$$ to either \begin{align} or \end{align}. Thus, TeX's "eyes" never actually see any instances of $$$.

Two types of input formats are handled by this setup:

  • Single-line input: "$$$ (body of equation) $$$" all on a single line: The input is converted to \begin{equation} (body of equation) \end{equation}. Note that because the entire equation consists of a single line, there's nothing to "align" the material to. Hence, this case employs the simpler equation environment.

  • Multi-line input: $$$ on separate lines, with the math material between these two lines: The opening instance of $$$ is converted to \begin{align}, and the closing instance of $$$ is converted to \end{align}.

Both "un-starred" and "starred" variants of $$$ are handled, with the latter producing un-numbered equation and align environments.

Examples of both single-line and multi-line input formats, both unstarred and starred ($$$*) are shown in the example below.

enter image description here

% !TEX TS-program = lualatex
\documentclass{article}
\usepackage{amsmath} % for 'align' environment

\usepackage{luacode}
\begin{luacode}
not_in_align = true -- Initially, *not* in an "align" env.
function dollars2align ( line )
   line = string.gsub ( line, "%$%$%$(%*?)(.+)%$%$%$(%*?)", 
                        "\\begin{equation%1}%2\\end{equation%3}" )
   if string.find ( line, "%$%$%$" ) then
      if not_in_align then
          line = string.gsub ( line , "%$%$%$(%*?)", "\\begin{align%1}" )
          not_in_align = false
      else
          line = string.gsub ( line , "%$%$%$(%*?)", "\\end{align%1}" )
          not_in_align = true
      end
   end
   return line
end
\end{luacode}
\AtBeginDocument{\directlua{luatexbase.add_to_callback (
   "process_input_buffer", dollars2align, "dollars2align" )}}


\begin{document}
$$$ \zeta(s) = \dfrac1{1^s} + \dfrac1{2^s} + \cdots $$$

$$$ 
a^2+b^2&=c^2\\
e^{i\pi}-1 &=0
$$$

$$$* 1+1=2 $$$*

$$$*
2+2&=4\\
4+4&=8
$$$*
\end{document}
Mico
  • 506,678
  • The fifth upvote of this answer produced my 27th “Necromancer” (silver-level) badge. More importantly, this latest badge is also the 1000th [!!] badge overall which I’ve earned on this site. Wow!! Many thanks to all who have made, and continue to make, TeX.SE such a special place. – Mico Feb 14 '18 at 17:00
  • +1 :-) StackExchange's auto-formatting (syntax highlighting) was getting confused and was annoying me; I've tweaked it slightly so that the Lua code gets highlighted properly… if you think it's confusing, feel free to revert. – ShreevatsaR Feb 14 '18 at 17:31
  • @ShreevatsaR - Many thanks for this edit! I didn't know until now that it's possible to change the site's auto-formatting/syntax highlighting method. For sure, I'll keep it in mind for future lua-related postings. – Mico Feb 14 '18 at 17:52
4
\documentclass{scrartcl}
\usepackage{fixltx2e}
\usepackage{mathtools}

\makeatletter
\catcode`\$=\active
\protected\def${\new@ifnextchar$\adhvaitha@checkthird\adhvaitha@math}
\protected\def\adhvaitha@checkthird${\new@ifnextchar$\adhvaitha@align\adhvaitha@dmath}
\protected\def\adhvaitha@math#1${\(#1\)}
\protected\def\adhvaitha@dmath#1$${\[#1\]}
\protected\def\adhvaitha@align$#1$$${\begin{align}#1\end{align}}
\makeatother

\begin{document}
Some math $x^2$

and a little more
$$ x^2 $$$x^2$$24$

\begin{figure}[h]
    \rule{2cm}{2cm}
    \caption{$x^2 +1$}
\end{figure}

$$$ \text{aligned} &= M \cdot A \cdot T \cdot H $$$
\end{document}

ADDITION: Same disclaimer as in egreg's answer applies here.

Manuel
  • 27,118
  • 2
    TeX.SX asked if I'm a script. I said I'm a human being. Question is: am I? – Manuel Dec 02 '14 at 21:08
  • Well, it doesn't work with my favourite: \begin{tabular}{>$l<$} x^2 \\ $Hello$\end{tabular} (package array needed). – yo' Dec 02 '14 at 21:10
  • This is the same as mine (less features, though). – egreg Dec 02 '14 at 21:11
  • @tohecz I'm not sure what happens there… You are using math mode inside a math column? In that case, I think that's abusing the code, better use \text{Hello} there. In any case \begin{tabular}{>{$}l<{$}} x^2 \end{tabular} doesn't work (which is a problem). I don't know why, but with only the $..$ and $$..$$ version it did work (now with $$$..$$$ it doesn't). – Manuel Dec 02 '14 at 21:17
  • 1
    @egreg I know. I was already writing it so I decided to post it. – Manuel Dec 02 '14 at 21:17
4

I understood that you need not to use align environment because of aligning more equations but because you need numbering of the single equation. IMHO it is bad idea because the vertical space between short paragraph followed by the equation is constructed from \abovedisplayskip, no from \abovedisplayshortskip.

This is a reason why I'm suggesting to use your $$$ ... $$$ markup only for numbered single equations. My following implementation puts the right vertical space above such display. There are only three lines of code.

\documentclass{article}
\usepackage{amsmath}

\everydisplay={\futurelet\next\numeqA}
\def\numeqA{\ifx\next$\expandafter\numeqB\fi} %$
\def\numeqB$#1$$${#1\eqno \csname make@display@tag\endcsname$$}

\begin{document}

The text:
$$$
   a=b 
$$$

The text:
\begin{align}
c=d
\end{align}

\end{document}

Please, compare the vertical space between the text and the equation in the first paragraph (it is OK) and in the second paragraph (it is bad). display

wipet
  • 74,238
  • How to do the same for $$$a&=b\\c&=d$$$, and use $$$...$$$ as a shortcut for \begin{align*}...\end{align*} @wipet? Thanks in advance! PS: I use LaTeX. – Basj Feb 14 '18 at 16:34