用于平方根和积分的 Unicode 宏

用于平方根和积分的 Unicode 宏

是否可以定义数学模式宏 √ 和 ∫,它们将按以下方式扩展:

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

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

答案1

很容易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}

在此处输入图片描述

请注意,和周围的括号π仅在使用时是必要的pdflatex,而不是使用 XeLaTeX。

以下是使用 Asana Math 的 XeLaTeX 示例:

\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}

在此处输入图片描述

答案2

以下是我想尝试使用的unicode数学宏:

\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}

给出:

在此处输入图片描述

在 emacs 中可以轻松输入 unicode 符号。我已将以下内容添加到~/.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") [?₀])

这是一个 emacs defun,它将缓冲区或行区域转换为 unicode 数学宏,并使用C--— 将其转换回来:

(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)

          )
         )

        ))))

工作原理如下:

在此处输入图片描述

相关内容