定义一个函数,用于在Emacs中对单词选择进行乳胶化(或粗体)

时间:2018-01-06 15:08:10

标签: emacs latex org-mode

我使用Emacs' org-mode在课堂上做笔记,然后我用乳胶输出。我发现org-mode的粗体和斜体的默认标记(分别为*/)非常有用,但我不得不停止使用它们以避免混淆组织导出器。我想有一个函数来斜体选择单词。换句话说,该函数应分别将\textit{}附加到选择的开头和结尾。

Org有一个在org.el中定义的函数,它执行此操作,称为org-emphasize(代码在下面发布),但它构造为仅与org标记一起使用。不幸的是,我不是很精通elisp来修改它来做我想做的事情。

关于如何构建我想要的功能的任何建议,将不胜感激。谢谢大家!

(defun org-emphasize (&optional char)
"Insert or change an emphasis, i.e. a font like bold or italic.
If there is an active region, change that region to a new emphasis.
If there is no region, just insert the marker characters and position
the cursor between them.
CHAR should be the marker character.  If it is a space, it means to
remove the emphasis of the selected region.
If CHAR is not given (for example in an interactive call) it will be
prompted for."
 (interactive)
(let ((erc org-emphasis-regexp-components)
 (string "") beg end move s)
 (if (org-region-active-p)
 (setq beg (region-beginning)
      end (region-end)
      string (buffer-substring beg end))
  (setq move t))

(unless char
  (message "Emphasis marker or tag: [%s]"
       (mapconcat #'car org-emphasis-alist ""))
  (setq char (read-char-exclusive)))
(if (equal char ?\s)
(setq s ""
      move nil)
  (unless (assoc (char-to-string char) org-emphasis-alist)
(user-error "No such emphasis marker: \"%c\"" char))
  (setq s (char-to-string char)))
(while (and (> (length string) 1)
    (equal (substring string 0 1) (substring string -1))
    (assoc (substring string 0 1) org-emphasis-alist))
  (setq string (substring string 1 -1)))
(setq string (concat s string s))
(when beg (delete-region beg end))
(unless (or (bolp)
    (string-match (concat "[" (nth 0 erc) "\n]")
              (char-to-string (char-before (point)))))
  (insert " "))
(unless (or (eobp)
    (string-match (concat "[" (nth 1 erc) "\n]")
              (char-to-string (char-after (point)))))
  (insert " ") (backward-char 1))
(insert string)
(and move (backward-char 1))))

1 个答案:

答案 0 :(得分:0)

我不知道是否值得定义一个新的lisp函数。这主要是这里描述的LaTeX问题:https://tex.stackexchange.com/questions/258394/make-block-of-text-italicized

您可以将此解决方案转置为Org,如下所示。首先,观察Org特殊块:

#+BEGIN_mySpecialBlock
 ...
#+END_mySpecialBlock

导出到LaTeX

\begin{mySpecialBlock}
 ...
\end{mySpecialBlock}

(有关详细信息,请参阅Org doc Special-blocks-in-LaTeX-export

然后将新的LaTeX环境定义为:

#+LATEX_HEADER: \newenvironment{itquote} {\begin{quote}\itshape} {\end{quote}\ignorespacesafterend}
#+LATEX_HEADER: \newenvironment{itpars}  {\par\itshape} {\par}
#+LATEX_HEADER: \newenvironment{bfquote} {\begin{quote}\bfseries} {\end{quote}\ignorespacesafterend}
#+LATEX_HEADER: \newenvironment{bfpars}  {\par\bfseries} {\par}

lipsum LaTeX包用于生成一些虚拟文本。

#+LATEX_HEADER: \usepackage{lipsum}

您可以使用此组织文档测试方法

#+LATEX: \lipsum[66]

#+BEGIN_bfquote
#+LATEX: \lipsum[66]
#+END_bfquote

#+BEGIN_bfpars
#+LATEX: \lipsum[66]
#+END_bfpars

#+BEGIN_itquote
#+LATEX: \lipsum[66]
#+END_itquote

#+BEGIN_itpars
#+LATEX: \lipsum[66]
#+END_itpars

LaTeX导出后,C-c C-e l o,你会得到类似的东西: enter image description here

为了更清楚,这里是完整的组织文档。对于真实文档,删除对lipsum的所有引用,这只是文本生成器。

#+LATEX_HEADER: \newenvironment{itquote} {\begin{quote}\itshape} {\end{quote}\ignorespacesafterend}
#+LATEX_HEADER: \newenvironment{itpars}  {\par\itshape} {\par}
#+LATEX_HEADER: \newenvironment{bfquote} {\begin{quote}\bfseries} {\end{quote}\ignorespacesafterend}
#+LATEX_HEADER: \newenvironment{bfpars}  {\par\bfseries} {\par}
#+LATEX_HEADER: \usepackage{lipsum}

#+TITLE: My Title

* My section

#+LATEX: \lipsum[66]

#+BEGIN_bfquote
#+LATEX: \lipsum[66]
#+END_bfquote

#+BEGIN_bfpars
#+LATEX: \lipsum[66]
#+END_bfpars

#+BEGIN_itquote
#+LATEX: \lipsum[66]
#+END_itquote

#+BEGIN_itpars
#+LATEX: \lipsum[66]
#+END_itpars