3

I would like to have something like §§ ... §§ that could replace \begin{align*} ... \end{align*} in my documents. How can this be done?

\documentclass{article}

\begin{document}

§§ x + 1 &= 0 §§

\end{document}

EDIT: I had suggested # previously but changed it to § since # already has a special meaning.

Clone
  • 533
  • 1
    $ has a special meaning in TeX, and so does #. Changing # like that will definitely break your document somewhere (not to mention that align wouldn't accept that, for the reason in the comment linked below). (You could make # behave like $ by doing \catcode`\#=3, but don't) – Phelype Oleinik Apr 02 '21 at 15:49
  • 2
  • @JoséCarlosSantos, nope, sorry. – Clone Apr 02 '21 at 16:05
  • @PhelypeOleinik, can I maybe use something like §§ instead? – Clone Apr 02 '21 at 16:05
  • What is wrong with ˙]and]`, a standard LaTeX notation for display math? – Zarko Apr 02 '21 at 16:37
  • Are you willing and able to compile your document with LuaLaTeX? – Mico Apr 02 '21 at 16:50
  • The symbol you want to use does not appear on my keyboard. – Peter Wilson Apr 02 '21 at 17:56
  • @Zarko, I could use that too if that could replace \begin{align*} ... \end{align*}. – Clone Apr 02 '21 at 17:57
  • @Mico, I have not heard about LuaLaTex before. I tried it with overleaf and it seem to compile a slower than pdfTex. – Clone Apr 02 '21 at 17:59
  • @PeterWilson, I have it under Esc key but I need to press Shift also at the same time. But its okay, the most important is the principle, we can change that to something else later. – Clone Apr 02 '21 at 17:59
  • @Clone - I have no idea how things compilation is done on Overleaf. On a standalone TeX installation, it's only the very first time ypu compile a document that you'll notice a slower performance for LuaLaTeX, which is due to a utility program having to build a font hash table. (One of the big differences between LuaLaTeX and pdfLaTeX is that the former can access system fonts.) After that first compilation run, there should be very little to no difference in compilation times. – Mico Apr 02 '21 at 18:09

2 Answers2

6

With Unicode engines it is pretty easy, because § is a single token, so you just have to look ahead and bail out if the next token is not another §. With pdfTeX is a bit trickier, because § is actually two tokens (two utf-8 octets), so when TeX sees a § it has to look two tokens to detect the §§. If both §§ are found, it just grabs everything to the next §§ and puts that inside an align* environment. Do not try to nest this. It won't work.

enter image description here

\documentclass{article}
\usepackage{amsmath}

\usepackage{iftex} \makeatletter \def\weird@align{\futurelet\let@token\weird@align@} \ifpdftex \AtBeginDocument{\DeclareUnicodeCharacter{00A7}{\weird@align}} \def\weird@align@{% \expandafter\ifx@firstoftwo§\let@token \expandafter\weird@align@@ \else \expandafter\textsection \fi} \def\weird@align@@#1{\futurelet@let@token@weird@align@} \def@weird@align@{% \expandafter\ifx@secondoftwo§@let@token \expandafter@weird@align@@ \else \expandafter\textsection \expandafter\let@token \fi} \def@weird@align@@#1#2§§{\begin{align}#2\end{align}} \else \let\normal@sect=§ \begingroup \catcode\§=13 \@firstofone{% \endgroup \AtBeginDocument{\catcode\§=13 \let§\weird@align} \def\weird@align@{% \ifx\let@token§% \expandafter@weird@align@@ \else \expandafter\normal@sect \fi} \def@weird@align@@#1#2§§{\begin{align}#2\end{align}} }% \fi

\begin{document}

§1

§¶

§§ x + 1 &= 0 \ x &= -1 §§

\end{document}

5

If you're willing and able to use LuaLaTeX, it's reasonably straightforward to set up a preprocessor-type action that will replace instances of §§ ... §§ with \begin{align} ... \end{align}.

enter image description here

\documentclass{article} 
\usepackage{amsmath}    % for 'align' env.
\usepackage{luacode}    % for '\luaexec' environment
\luaexec{ % see https://tex.stackexchange.com/a/240185/5001
    local in_display_math = false
    function replace§§ ( x )
      if not in_display_math then
         in_display_math = true
         return "\\begin{align}"
      else
         in_display_math = false
         return "\\end{align}"
      end
    end
    function replace_double_para ( s )
       s = s:gsub ( "§§(.-)§§" , "\\begin{align} \%1 \\end{align}" )
       s = s:gsub ( "§§" , replace§§ )
       return s
    end
}
% define a couple of LaTeX utility macros
\newcommand\ReplaceDoubleParaOn{\directlua{luatexbase.add_to_callback( 
   "process_input_buffer", replace_double_para, "replace_double_para" )}}
\newcommand\ReplaceDoubleParaOff{\directlua{luatexbase.remove_from_callback( 
   "process_input_buffer", "replace_double_para" )}}

\begin{document} \ReplaceDoubleParaOn % activate the Lua function

§§ 5+5 &=10 §§

§§ 1+1 &= 2 \ 3+3 &= 6 \ 9+9 &=18 §§

\ReplaceDoubleParaOff % deactivate the Lua function §§§§§§§§§§§§§§§§ \end{document}

Mico
  • 506,678