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}}
  • Welcome to TeX.SX! You can have a look at our starter guide to familiarize yourself further with our format. – yo' Dec 2 '14 at 20:23
  • 14
    this is a very bad idea! also, your suggested definition of \ba won't work; that is documented as section 6 in the technical notes to the amsmath package – barbara beeton Dec 2 '14 at 20:59
  • 1
    TeX doesn't behave like any other language that you seem to replicate here. Neither $$$ nor that \ba definition will save you in the long run and you would go nuts when you are trying to debug a strange case. – percusse Dec 2 '14 at 21:00
  • 9
    You should not use align for one-line equations, so you would not want a shorthand for align in this case. – David Carlisle Dec 2 '14 at 21:22
  • 1
    @barbarabeeton the above comment is a present for you:-) – David Carlisle Dec 2 '14 at 21:53
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

  • 31
    +1 for the disclaimer. i'd also warn against being anywhere near an active volcano or a plate boundary. – barbara beeton Dec 2 '14 at 21:13
  • 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 2 '14 at 21:30
  • 7
    @Adhvaitha The function is already there, easy to use and much less obfuscated: \begin{align}...\end{align}. – egreg Dec 2 '14 at 21:37
  • 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 2 '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 2 '14 at 21:46
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

9

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.

  • 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 3 '14 at 9: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 3 '14 at 16:05
5

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}
  • 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.

  • 2
    TeX.SX asked if I'm a script. I said I'm a human being. Question is: am I? – Manuel Dec 2 '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 2 '14 at 21:10
  • This is the same as mine (less features, though). – egreg Dec 2 '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 2 '14 at 21:17
  • 1
    @egreg I know. I was already writing it so I decided to post it. – Manuel Dec 2 '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

  • 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

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.