org-download 在 orgmode 中插入图片, 导出 PDF 的时候怎么解决路径问题?

如图所示 上面是 org 文档中的形式, 下面是 PDF 文件中的形式

telegram-cloud-photo-size-5-6244310569649811727-x

我的 org-download 设置

image

1 个赞

可以先导出成tex看看图片是怎么引用的

image 是链接

https://github.com/abo-abo/org-download/issues/151

以前碰到过类似的问题,主要就是下划线被escape了。

试试使用attach作为org-download-method试试看呢?

org 的 attach 我还要研究一下, 以前没用过. 最好的话就是在当前文件夹下创建 img 文件夹这种方法了, 方便随时管理图片文件,关于 attach 论坛里面有类似的解决方法

如果是下划线被 escape 的话我建议这样设

(setq org-use-sub-superscripts '{}
      org-export-with-sub-superscripts '{})
1 个赞

没有用, 还是显示不出来图片, 最笨的方法是自己手动保存图片, 然后写成[[file:./…]] 插入, 这样在任何格式下都很稳定.

我的方案如下 目录结构

.
├── attached 保存图片
├── exports   保存导出的文件,latex模板也放这里
└── src          保存org文件

配置如下:

(use-package org-download
    :ensure t
    :config
    (setq org-download-method 'directory)
    ;;设置org download保存图片的路径,org文件保存在src子目录中,图片保存到同级的
    ;;org-download-dirs子目录中,导出的Latex文件到同级的exports子目录中
    ;;导出到exports目录下需要在org-mode文件中指定#+EXPORT_FILE_NAME: ../exports/vanilla
    (defconst org-download-root-directory "../attached/")
    (defun set-org-download-directory ()
      (create-directory-if-not-exists org-download-root-directory)
      (make-local-variable 'org-download--dir)
      (setq org-download-image-dir (concat  org-download-root-directory (buffer-name)))
      (setq org-download-heading-lvl nil)
      (setq org-attach-directory  org-download-image-dir)
      (message "set-org-download-directory")
      )

    (add-hook 'org-mode-hook #'set-org-download-directory)
    (add-hook 'dired-mode-hook 'org-download-enable)
    (define-key org-mode-map (kbd "s-M-v") 'org-download-clipboard)
    )

org文件最前面

#+TITLE: xxxx
#+EXPORT_FILE_NAME: ../exports/xxxxx
#+LATEX_CLASS: elegantbook
#+LATEX_COMPILER: xelatex
#+LaTeX_HEADER: \usemintedstyle{emacs}
#+OPTIONS: \n:nil
#+OPTIONS: H:5  toc:nil  title:nil
#+LANGUAGE: zh


#+begin_export latex
\maketitle
\frontmatter
\tableofcontents
\mainmatter
#+end_export
2 个赞

可以再分享一下你的 org 导出设置吗?

;;export
;; 使用xelatex一步生成PDF
;; (setq org-latex-to-pdf-process
;;       '("xelatex -interaction -shell-escape nonstopmode %f"
;;         "xelatex -interaction -shell-escape nonstopmode %f"))
(with-eval-after-load 'org
(setq org-latex-to-pdf-process
      '("xelatex  -shell-escape  -8bit %f"
        "xelatex  -shell-escape  -8bit %f"))
;; code执行免应答(Eval code without confirm)
(setq org-confirm-babel-evaluate nil)
;; Auctex
(setq TeX-auto-save t)
(setq TeX-parse-self t)
(setq-default TeX-master nil)

(defun org-mode-article-modes ()
  (reftex-mode t)
  (and (buffer-file-name)
       (file-exists-p (buffer-file-name))
       (reftex-parse-all)))
(add-hook 'org-mode-hook
          (lambda ()
            (if (member "REFTEX" org-todo-keywords-1)
                (org-mode-article-modes))))
(unless (boundp 'org-export-latex-classes)
  (setq org-export-latex-classes nil))

(with-eval-after-load 'org
  (org-babel-do-load-languages 'org-babel-load-languages
                               '((dot . t)
                                 (maxima . t)
                                 (python . t)))
  (setq org-babel-python-command "python3"))

(with-eval-after-load 'ox-latex
  (add-to-list 'org-latex-classes
               '("elegantbook"
                 "\\documentclass[cn,10pt]{elegantbook}



\\extrainfo{Victory won\\rq t come to us unless we go to it. --- M. Moore}
\\setcounter{tocdepth}{3}
\\logo{logo-blue.png}
\\cover{cover.jpg}


\\usepackage{fontspec}
\\setmonofont{DejaVu Sans Mono}
\\usepackage{minted}
\\newminted{cpp}{frame=single,linenos,breaklines}
\\newminted{java}{frame=single,linenos,,breaklines}
\\newminted{shell}{frame=single,linenos,breaklines}
\\newminted{ruby}{frame=single,linenos,breaklines}
\\newminted{typescript}{frame=single,linenos,breaklines}
\\newminted{js}{frame=single,linenos,breaklines}
\\newminted{sql}{frame=single,linenos,breaklines}
\\newminted{common-lisp}{frame=single,linenos,breaklines}
\\newminted{lisp}{frame=single,linenos,breaklines}
\\newminted{yaml}{frame=single,linenos,breaklines}
\\newminted{xml}{frame=single,linenos,breaklines}
\\newminted{tex}{frame=single,linenos,breaklines}
\\newminted{rust}{frame=single,linenos,breaklines}
\\newminted{python}{frame=single,linenos,breaklines}
\\newminted{html}{frame=single,linenos,breaklines}
\\newminted{groovy}{frame=single,linenos,breaklines}
\\newminted{go}{frame=single,linenos,breaklines}
\\newminted{c++}{frame=single,linenos,breaklines}
\\newminted{cmake}{frame=single,linenos,breaklines}
\\newminted{make}{frame=single,linenos,breaklines}
\\newminted{abap}{frame=single,linenos,breaklines}


[NO-DEFAULT-PACKAGES]
[NO-PACKAGES]

"
                 ("\\chapter{%s}" . "\\chapter*{%s}")
                 ("\\section{%s}" . "\\section*{%s}")
                 ("\\subsection{%s}" . "\\subsection*{%s}")
                 ("\\subsubsection{%s}" . "\\subsubsection*{%s}")
                 ("\\paragraph{%s}" . "\\paragraph*{%s}")
                 ("\\subparagraph{%s}" . "\\subparagraph*{%s}"))
               )



;; 导出Beamer的设置
;; allow for export=>beamer by placing #+LaTeX_CLASS: beamer in org files
;;-----------------------------------------------------------------------------
(add-to-list 'org-export-latex-classes
               ;; beamer class, for presentations
               '("beamer"
                 "\\documentclass[11pt,professionalfonts]{beamer}
\\mode
\\usetheme{{{{Warsaw}}}}
%\\usecolortheme{{{{beamercolortheme}}}}

\\beamertemplateballitem
\\setbeameroption{show notes}
\\usepackage{graphicx}
\\usepackage{tikz}
\\usepackage{xcolor}
\\usepackage{titletoc}
\\usepackage{ctex}
\\usepackage{xeCJK}
\\usepackage{amsmath}
\\usepackage{lmodern}
\\usepackage{fontspec,xunicode,xltxtra}
\\usepackage{polyglossia}
\\setmainfont{Times New Roman}
\\setCJKmainfont{DejaVu Sans YuanTi}
\\setCJKmonofont{DejaVu Sans YuanTi Mono}
\\usepackage{verbatim}
\\usepackage{listings}
\\institute{{{{beamerinstitute}}}}
\\subject{{{{beamersubject}}}}"
                 ("\\section{%s}" . "\\section*{%s}")
                 ("\\begin{frame}[fragile]\\frametitle{%s}"
                  "\\end{frame}"
                  "\\begin{frame}[fragile]\\frametitle{%s}"
                  "\\end{frame}")))

;; Settings to export code with `minted' instead of `verbatim'.
(setq org-export-latex-listings t)
(setq org-latex-listings 'minted
      org-latex-packages-alist '(("" "minted"))
      ))

(setq org-latex-custom-lang-environments
      '(
        (C "cppcode")
        (java "javacode")
        (emacs-lisp "lispcode")
        (shell "shellcode")
        (ruby "rubycode")
        (js "jscode")
        (typescript "typescriptcode")
        (js "jscode")
        (sql "sqlcode")
        (yaml "yamlcode")
        (xml "xmlcode")
        (tex "texcode")
        (rust "rustcode")
        (python "pythoncode")
        (html "htmlcode")
        (groovy "groovycode")
        (go "gocode")
        (c++ "cppcode")
        (cmake "cmakecode")
        (make "makecode")
        (abap "abapcode")
        ))

;; 各种Babel语言支持
(org-babel-do-load-languages
 'org-babel-load-languages
 '((R . t)
   (emacs-lisp . t)
   (matlab . t)
   (C . t)
   (perl . t)
   (ditaa . t)
   (python . t)
   (haskell . t)
   (dot . t)
   (latex . t)
   (js . t)
   (java . t)
   (shell . t)
   ))
)

(provide 'init-org-export-to-latex)
4 个赞

下面是我在 doomemacs 中的配置

(use-package! org-download
  :hook ((org-mode dired-mode) . org-download-enable)
  ;; :init
  :config
  (setq-default org-download-method 'directory)
  (setq-default org-download-image-dir "./img")
  (setq-default org-download-heading-lvl 'nil)
  ;;FIXME (setq org-download-screenshot-method "screencapture -i %s")
  )

暂时可供参考

我再贴一段不使用 org-download 的更好的解决方法

(use-package! emacs
  :ensure nil
  :after org
  :bind (:map org-mode-map
              ("s-V" . my/org-insert-clipboard-image))
  :config
  (defun my/org-insert-clipboard-image (width)
    "create a time stamped unique-named file from the clipboard in the sub-directory
 (%filename.assets) as the org-buffer and insert a link to this file."
    (interactive (list
                  (read-string (format "Input image width, default is 800: ")
                               nil nil "800")))
    ;; 设置图片存放的文件夹位置为 `当前Org文件同名.assets'
    (setq foldername (concat (file-name-base (buffer-file-name)) ".assets/"))
    (if (not (file-exists-p foldername))
        (mkdir foldername))
    ;; 设置图片的文件名,格式为 `img_年月日_时分秒.png'
    (setq imgName (concat "img_" (format-time-string "%Y%m%d_%H%M%S") ".png"))
    ;; 图片文件的相对路径
    (setq relativeFilename (concat (file-name-base (buffer-name)) ".assets/" imgName))
    ;; 根据不同的操作系统设置不同的命令行工具
    (cond ((string-equal system-type "gnu/linux")
           (shell-command (concat "xclip -selection clipboard -t image/png -o > " relativeFilename)))
          ((string-equal system-type "darwin")
           (shell-command (concat "pngpaste " relativeFilename))))
    ;; 给粘贴好的图片链接加上宽度属性,方便导出
    (insert (concat "\n#+DOWNLOADED: screenshot @ "
                    (format-time-string "%Y-%m-%d %a %H:%M:%S" (current-time))
                    "\n#+CAPTION: \n#+ATTR_ORG: :width "
                    width
                    "\n#+ATTR_LATEX: :width "
                    (if (>= (/ (string-to-number width) 800.0) 1.0)
                        "1.0"
                      (number-to-string (/ (string-to-number width) 800.0)))
                    "\\linewidth :float nil\n"
                    "#+ATTR_HTML: :width "
                    width
                    "\n[[file:" relativeFilename "]]\n"))
    ;; 重新显示一下图片
    (org-redisplay-inline-images)
    )
  )