是否可以在 LaTeX (2e) 中创建宏或其他处理命令,在文本模式下在单个字符后添加不间断空格?
例子:
A new example here will add a "tilde", after single letter, but will skip math mode.
$12 \times A = 128$
应产生:
A~new example here will add a~"tilde", after single letter, but will skip math mode.
$12 \times A = 128$
我知道我可以使用类似这样的正则表达式:s/\(\<[a-z]\>\)[ ]/\1\~/g
,但是这个也会在数学模式和列表/逐字模式下改变文本。
答案1
在 TeX 中执行此操作会有点脆弱:我会按照您的建议在编辑器中使用正则表达式进行替换。(如果您只想~
在文本中创建一个并在数学中创建一个普通空格,这会更容易,但让它在列表和逐字中工作会更难)
例如给定
\documentclass{article}
\usepackage{amsmath}
\begin{document}
A new example here will add a "tilde", after single letter, but will skip math mode.
$12 \times A = 128$
\[ a b c = 123\]
and a bit nore text
\begin{align}
a &= b\\
x & = y
\end{align}
\begin{verbatim}
A new example here will add a "tilde", after single letter,
but will skip math mode.
$12 \times A = 128$
\[ a b c = 123\]
and a bit nore text
\begin{align}
a &= b\\
x & = y
\end{align}
\end{verbatim}
A new example here will add a "tilde", after single letter, but will skip math mode.
$12 \times A = 128$
\[ a b c = 123\]
and a bit nore text
\begin{align}
a &= b\\
x & = y
\end{align}
\begin{verbatim}
A new example here will add a "tilde", after single letter,
but will skip math mode.
$12 \times A = 128$
\[ a b c = 123\]
and a bit nore text
\begin{align}
a &= b\\
x & = y
\end{align}
\end{verbatim}
\end{document}
M-x addtilde
emacs 中的命令产生
\documentclass{article}
\usepackage{amsmath}
\begin{document}
A~new example here will add a~"tilde", after single letter, but will skip math mode.
$12 \times A = 128$
\[ a b c = 123\]
and a~bit nore text
\begin{align}
a &= b\\
x & = y
\end{align}
\begin{verbatim}
A new example here will add a "tilde", after single letter,
but will skip math mode.
$12 \times A = 128$
\[ a b c = 123\]
and a bit nore text
\begin{align}
a &= b\\
x & = y
\end{align}
\end{verbatim}
A~new example here will add a~"tilde", after single letter, but will skip math mode.
$12 \times A = 128$
\[ a b c = 123\]
and a~bit nore text
\begin{align}
a &= b\\
x & = y
\end{align}
\begin{verbatim}
A new example here will add a "tilde", after single letter,
but will skip math mode.
$12 \times A = 128$
\[ a b c = 123\]
and a bit nore text
\begin{align}
a &= b\\
x & = y
\end{align}
\end{verbatim}
\end{document}
其中addtilde
定义为
(defun hidespaceenv (e)
(goto-char (point-min))
(while (re-search-forward
(if (string-equal e "[") "\\\\\\["
(if (string-equal e "$") "\\$"
(concat "\\\\begin{" e "}")))
nil 1)
(while (re-search-forward "\\([a-zA-Z]\\)\\(\\s-+\\)\\|\\(\\\\begin\\)" (save-excursion(re-search-forward
(if (string-equal e "[") "\\\\\\]"
(if (string-equal e "$") "\\$"
(concat "\\\\end{" e "}")))
nil 1) (point)) 1)
(replace-match "\\1SPACE@@\\2\\3@@" t))))
(defun addtilde ()
(interactive)
(mapcar `hidespaceenv (list "verbatim" "align" "equation" "[" "$"))
(goto-char (point-min))
(while (re-search-forward "\\(\\(^\\|\\s-+\\)[a-zA-Z]\\)\\s-+" nil 1)
(replace-match "\\1~"))
(goto-char (point-min))
(while (re-search-forward "SPACE@@\\(\\s-+\\|\\\\begin\\)@@" nil 1)
(replace-match "\\1")))