点击包名是否打开对应的样式文件?

点击包名是否打开对应的样式文件?

我认为,如果我可以在我的 LaTeX 编辑器中单击包的名称,那就太好了...

\usepackage{blob}

...并且相应的样式文件blob.sty将被打开。

有没有 LaTeX 编辑器有这个功能?我自己主要使用 Aquamacs/AucTeX,但也对其他编辑器感兴趣。我认为实现起来会有点困难,因为blob.sty可能

  • 在当前工作目录中,
  • 在系统texmf目录中,或
  • 在用户texmf目录中。

答案1

Emacs 开发版本现在已内置此功能(请参阅这次提交)。这将在 Emacs 25.1 中上线。你只需要发出M-x ffap RET。下面是我的旧答案,供参考。


从David的回答开始,我编写了以下函数(它们需要AUCTeX)。

(defun mg-TeX-kpsewhich-find-file (&optional name)
  "Visit file associated to NAME searching for it with kpsewhich.
If NAME is nil prompt for a file name.  If there is an active
region, use it as initial input.  When it is called with
\\[universal-argument] prefix, visit file in another window, in
the current one otherwise."
  (interactive)
  (if (executable-find "kpsewhich")
      (let* ((fun (if current-prefix-arg 'find-file-other-window 'find-file))
         (default-directory (TeX-master-directory))
         (name (or name (TeX-read-string
                 "File name: "
                 (if (TeX-active-mark)
                 (buffer-substring-no-properties
                  (region-beginning) (region-end))))))
         (file (replace-regexp-in-string
            "[\n\r]*\\'" ""
            (shell-command-to-string (concat "kpsewhich " name)))))
    (if (and (not (zerop (length file))) (file-exists-p file))
        (funcall fun file)
      (message (concat "File " name " not found."))))
    (message "Kpsewhich not available.")))
(define-key TeX-mode-map (kbd "C-c k") 'mg-TeX-kpsewhich-find-file)

(defun mg-LaTeX-find-file-at-point ()
  "Visit LaTeX file searching for it with kpsewhich.
File basename is guessed from text around point and its extension
is guessed from current macro.  When it is called with
\\[universal-argument] prefix, visit file in another window, in
the current one otherwise.

See also `mg-TeX-kpsewhich-find-file'."
  (interactive)
  (let* ((file-name-regexp "-~/A-Za-z0-9_.$#%:+")
     ;; Get filename at point.
     (name
      ;; Check whether character at point is a valid file name character.
      (if (string-match (concat "[" file-name-regexp "]")
                (string (char-after)))
          (save-excursion
             (skip-chars-backward file-name-regexp)
             (looking-at (concat "\\([" file-name-regexp "]+\\)"))
             (TeX-match-buffer 1))))
     ;; Get current macro once.
     (current-macro (TeX-current-macro))
     ;; Guess file extension based on current macro.
     (extension (cond
             ((or (equal "usepackage" current-macro)
              (equal "RequirePackage" current-macro)
              (equal "RequirePackageWithOptions" current-macro))
              ".sty")
             ((or (equal "documentclass" current-macro)
              (equal "documentstyle" current-macro)
              (equal "LoadClass" current-macro)
              (equal "LoadClassWithOptions" current-macro))
              ".cls")
             ((equal "include" current-macro) ".tex")
             ((equal "input" current-macro)
              ;; `input' macro accepts a file name with extension, in
              ;; that case use an empty but non-nil extension.
              (if (and name (file-name-extension name)) "" ".tex"))
             ((equal "bibliography" current-macro) ".bib")
             ((equal "addbibresource" current-macro) "")
             (t nil))))
    (if (and name extension)
    (mg-TeX-kpsewhich-find-file (concat name extension))
      (message "Cannot guess file name at point."))))
(define-key LaTeX-mode-map (kbd "C-c f") 'mg-LaTeX-find-file-at-point)

如您所见, (我mg-LaTeX-find-file-at-point的键绑定C-c f:)不仅适用于,还\usepackage适用于\RequirePackage{,WithOptions}、、、、、、和。您可以使用或搜索任何 (La)TeX 文件,如果存在活动区域​​,它将被用作初始输入。这对于不为人所知的宏很有用,例如:您标记输入文件名并使用搜索它。\document{class,style}\LoadClass{,WithOptions}\include\input\bibliogaphy\addbibresourcekpsewhichM-x mg-TeX-kpsewhich-find-fileC-c kmg-LaTeX-find-file-at-point\lstinputlistingC-c k

答案2

粘点在附近的某处

\usepackage{array,colortbl}

并且将为数组和 colortbl 包打开两个缓冲区,如果你M-x getpackage 这样做,你可以将其绑定到你选择的键

(defun getpackage ()
(interactive)
(search-backward "\\")
(re-search-forward "usepackage[^{}]*{" nil t)
(while (looking-at "\\s-*,*\\([a-zA-Z0-9]+\\)")
(re-search-forward "\\s-*,*\\([a-zA-Z0-9]+\\)" nil 1)
(save-excursion
  (find-file-other-window (replace-regexp-in-string "[\n\r ]*" "" (shell-command-to-string (concat "kpsewhich " (match-string 1) ".sty")))))))

相关内容