4

Is it possible to define math-mode macros √ and ∫ which would expand in the following way:

√a       ≡ \sqrt{a}
√{a + b} ≡ \sqrt{a + b}

∫     ≡ \int
∫_a   ≡ \int\limits_a
∫^a   ≡ \int\limits^a
∫_a^b ≡ \int\limits_a^b

?

  • Yes, with the newunicodechar package you can do \newunicodechar{√}{\sqrt} and \newunicodechar{∫}{\int} (don't add \limits, please). – egreg Apr 16 '15 at 16:51
  • @egreg: But I want \limits -- it is ugly without them. – Adobe Apr 16 '15 at 16:52
  • @egreg: \newunicodechar{√}{\sqrt} breaks √ in text mode. I don't think I'll need it, but it is no good to break things. – Adobe Apr 16 '15 at 16:55
  • @egreg: \newunicodechar{∫}{\int} doesn't work at all. – Adobe Apr 16 '15 at 16:58
7

It's easy with newunicodechar

\documentclass{article}

\usepackage[intlimits]{amsmath}

\usepackage{ifxetex}

\ifxetex
  \usepackage{unicode-math}
  \removelimits{\int}
\else
  \usepackage[T1]{fontenc}
  \usepackage[utf8]{inputenc}
\fi

\usepackage{newunicodechar}

\newunicodechar{√}{\sqrt}
\ifxetex\else
  % these are already available with unicode-math
  \newunicodechar{∫}{\int}
  \newunicodechar{∞}{\infty}
  \newunicodechar{π}{\pi}
\fi

\begin{document}

\[
∫_{-∞}^{∞}e^{-x^2}\,dx = √{π}
\]

\end{document}

enter image description here

Note that the braces around and π are necessary only with pdflatex, not with XeLaTeX.

Here's a XeLaTeX only example with Asana Math:

\documentclass{article}

\usepackage{amsmath}
\usepackage{unicode-math}

\usepackage{newunicodechar}

\newunicodechar{√}{\sqrt}

\setmainfont{TeX Gyre Pagella}
\setmathfont{Asana Math}
\removenolimits{\int}

\begin{document}

\[
∫_{-∞}^∞ e^{-x^2}\,dx = √π
\]

\end{document}

enter image description here

  • For some reason integral doesn't work if I use \setmathfont{Asana-Math.otf}. Compiler hangs at texmf-dist/tex/latex/tipa/t3cmr.fd. – Adobe Apr 16 '15 at 17:06
  • @Adobe Update your TeX distribution; as you see, I have added a working example. – egreg Apr 16 '15 at 17:12
4

Here are unicode math macros I think I'll try to use:

\documentclass{article}

\usepackage{amsmath}
\usepackage{unicode-math}
\setmathfont{Asana Math}

\usepackage{newunicodechar}

% roots:
\newunicodechar{√}{\sqrt}
\newunicodechar{∛}{\sqrt[3]}
\newunicodechar{∜}{\sqrt[4]}

% full differential:
\newunicodechar{d}{\,\mathrm{d}}

% superscripts:
\newunicodechar{⁰}{^0}
\newunicodechar{¹}{^1}
\newunicodechar{²}{^2}
\newunicodechar{³}{^3}
\newunicodechar{⁴}{^4}
\newunicodechar{⁵}{^5}
\newunicodechar{⁶}{^6}
\newunicodechar{⁷}{^7}
\newunicodechar{⁸}{^8}
\newunicodechar{⁹}{^9}

% subscripts:
\newunicodechar{₀}{_0}
\newunicodechar{₁}{_1}
\newunicodechar{₂}{_2}
\newunicodechar{₃}{_3}
\newunicodechar{₄}{_4}
\newunicodechar{₅}{_5}
\newunicodechar{₆}{_6}
\newunicodechar{₇}{_7}
\newunicodechar{₈}{_8}
\newunicodechar{₉}{_9}

% use \limits by default:
\removenolimits{\int}
\removenolimits{\iint}
\removenolimits{\iiint}
\removenolimits{\oint}
\removenolimits{\oiint}
\removenolimits{\oiiint}

% left-rigth:
\newunicodechar{(}{\left (}
\newunicodechar{)}{\right )}
\newunicodechar{[}{\left [}
\newunicodechar{[}{\right ]}
\newunicodechar{{}{\left \lbrace}
\newunicodechar{}}{\right \rbrace}
\newunicodechar{<}{\left \langle}
\newunicodechar{>}{\right \rangle}

\begin{document}

$√a ~~ √{a + b}$

$dx ~~ dx$

$x¹ ~~ x₁$

$∫_0^1$

$(\frac{1}{2})$

\begin{equation}
   ∫_0^1
\end{equation}

\end{document}

Gives:

enter image description here

One can easily enter unicode symbols in emacs. I've added the following to ~/.emacs:

;; roots:
(define-key 'iso-transl-ctl-x-8-map (kbd "C-S-s") [?√])
(define-key 'iso-transl-ctl-x-8-map (kbd "C-3 C-S-s") [?∛])
(define-key 'iso-transl-ctl-x-8-map (kbd "C-4 C-S-s") [?∜])

;; integrals:
(define-key 'iso-transl-ctl-x-8-map (kbd "C-S-i") [?∫])
(define-key 'iso-transl-ctl-x-8-map (kbd "C-2 C-S-i") [?∬])
(define-key 'iso-transl-ctl-x-8-map (kbd "C-3 C-S-i") [?∭])
(define-key 'iso-transl-ctl-x-8-map (kbd "C-S-c") [?∮])
(define-key 'iso-transl-ctl-x-8-map (kbd "C-2 C-S-c") [?∯])
(define-key 'iso-transl-ctl-x-8-map (kbd "C-3 C-S-c") [?∰])

(define-key 'iso-transl-ctl-x-8-map (kbd "C-S-d") [?d])

;; left-right:
(define-key 'iso-transl-ctl-x-8-map (kbd "(") [?(])
(define-key 'iso-transl-ctl-x-8-map (kbd ")") [?)])
(define-key 'iso-transl-ctl-x-8-map (kbd "[") [?[])
(define-key 'iso-transl-ctl-x-8-map (kbd "]") [?]])
(define-key 'iso-transl-ctl-x-8-map (kbd "{") [?{])
(define-key 'iso-transl-ctl-x-8-map (kbd "}") [?}])
(define-key 'iso-transl-ctl-x-8-map (kbd "<") [?<])
(define-key 'iso-transl-ctl-x-8-map (kbd ">") [?>])


(define-key 'iso-transl-ctl-x-8-map (kbd "!") [?¹]) ;; S-1
(define-key 'iso-transl-ctl-x-8-map (kbd "@") [?²]) ;; S-2
(define-key 'iso-transl-ctl-x-8-map (kbd "#") [?³]) ;; S-3
(define-key 'iso-transl-ctl-x-8-map (kbd "$") [?⁴]) ;; S-4
(define-key 'iso-transl-ctl-x-8-map (kbd "%") [?⁵]) ;; S-5
(define-key 'iso-transl-ctl-x-8-map (kbd "^") [?⁶]) ;; S-6
(define-key 'iso-transl-ctl-x-8-map (kbd "&") [?⁷]) ;; S-7
(define-key 'iso-transl-ctl-x-8-map (kbd "*") [?⁸]) ;; S-8
(define-key 'iso-transl-ctl-x-8-map (kbd "C-9") [?⁹]) ;; S-C-9
(define-key 'iso-transl-ctl-x-8-map (kbd "C-0") [?⁰]) ;; S-C-0

(define-key 'iso-transl-ctl-x-8-map (kbd "M-1") [?₁])
(define-key 'iso-transl-ctl-x-8-map (kbd "M-2") [?₂])
(define-key 'iso-transl-ctl-x-8-map (kbd "M-3") [?₃])
(define-key 'iso-transl-ctl-x-8-map (kbd "M-4") [?₄])
(define-key 'iso-transl-ctl-x-8-map (kbd "M-5") [?₅])
(define-key 'iso-transl-ctl-x-8-map (kbd "M-6") [?₆])
(define-key 'iso-transl-ctl-x-8-map (kbd "M-7") [?₇])
(define-key 'iso-transl-ctl-x-8-map (kbd "M-8") [?₈])
(define-key 'iso-transl-ctl-x-8-map (kbd "M-9") [?₉])
(define-key 'iso-transl-ctl-x-8-map (kbd "M-0") [?₀])

Here's an emacs defun which converts buffer or region of lines to unicode math macros, and with C-- — converts it back:

(defun tex-unicode-maths ()
  "Converts equation to unidoce math. With C-- un-unicodes it back.
   Acts on buffer or region of lines."
  (interactive)
  (save-excursion
    (save-restriction
      (let ( start end pairs

                   (common-pairs '(
                                   ("\\\\sqrt" "√")
                                   ("\\\\sqrt[3]" "∛")
                                   ("\\\\sqrt[4]" "∜")
                                   ("_0" "₀")
                                   ("_1" "₁")
                                   ("_2" "₂")
                                   ("_3" "₃")
                                   ("_4" "₄")
                                   ("_5" "₅")
                                   ("_6" "₆")
                                   ("_7" "₇")
                                   ("_8" "₈")
                                   ("_9" "₉")
                                   ))

                   (unicode-pairs '(
                                    ("\\^0" "⁰")
                                    ("\\^1" "¹")
                                    ("\\^2" "²")
                                    ("\\^3" "³")
                                    ("\\^4" "⁴")
                                    ("\\^5" "⁵")
                                    ("\\^6" "⁶")
                                    ("\\^7" "⁷")
                                    ("\\^8" "⁸")
                                    ("\\^9" "⁹")
                                    ("\\\\,[[:blank:]]*\\\\mathrm{d}"  "d")
                                    ("\\\\left[[:blank:]]*("           "(")
                                    ("\\\\right[[:blank:]]*)"          ")")
                                    ("\\\\left[[:blank:]]*\\["           "[")
                                    ("\\\\right[[:blank:]]*\\]"          "]")
                                    ("\\\\left[[:blank:]]*\\\\lbrace"  "{")
                                    ("\\\\right[[:blank:]]*\\\\rbrace"  "}")
                                    ("\\\\left[[:blank:]]*\\\\langle"  "<")
                                    ("\\\\right[[:blank:]]*\\\\rangle" ">")
                                    ))

                   (un-unicode-pairs '(
                                       ("^0" "⁰")
                                       ("^1" "¹")
                                       ("^2" "²")
                                       ("^3" "³")
                                       ("^4" "⁴")
                                       ("^5" "⁵")
                                       ("^6" "⁶")
                                       ("^7" "⁷")
                                       ("^8" "⁸")
                                       ("^9" "⁹")
                                       ("\\\\,\\\\mathrm{d}"   "d")
                                       ("\\\\left ("           "(")
                                       ("\\\\right )"          ")")
                                       ("\\\\left \\["         "[")
                                       ("\\\\right \\]"        "]")
                                       ("\\\\left \\\\lbrace"  "{")
                                       ("\\\\right \\\\rbrace"  "}")
                                       ("\\\\left \\\\langle"  "<")
                                       ("\\\\right \\\\rangle" ">")
                                       ))

                   (case-fold-search nil)
                   )
        (if (use-region-p)
            (progn (setq start (region-beginning)) ;; then
                   (setq end (region-end))
                   (goto-char start)
                   (setq start (line-beginning-position))
                   (goto-char end)
                   (setq end (line-end-position)))
          (progn (setq start (point-min)) ;; else
                 (setq end (point-max))))

        (narrow-to-region start end)

        ;; despite we have a single argument here the cond strutcture
        ;; is used since it allows for easy extension for more args:
        (cond
         ((equal current-prefix-arg '-)

          (setq pairs (append common-pairs un-unicode-pairs))

          (mapc
           (lambda (currentPair)
             (goto-char (point-min))
             (while (search-forward-regexp (elt currentPair 1) nil t)
               (replace-match (elt currentPair 0) t nil)))
           pairs)
          )

         (t

          (setq pairs (append common-pairs unicode-pairs))

          (mapc
           (lambda (currentPair)
             (goto-char (point-min))
             (while (search-forward-regexp (elt currentPair 0) nil t)
               (replace-match (elt currentPair 1) t nil)))
           pairs)

          )
         )

        ))))

Here's how it works:

enter image description here

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.