【问题标题】:How to label code block evaluations in orgmode on html export?如何在 html 导出的 orgmode 中标记代码块评估?
【发布时间】:2016-06-26 00:47:55
【问题描述】:

如果我在 orgmode 中有一个代码块,我可以使用 :exports both 选项导出它的评估。

#+begin_src cpp -n :includes <iostream> :exports both
std::cout << "Hello there";
#+end_src

#+RESULTS:
: Hello there

当我导出到 html (C-c C-e h o) 时,结果块跟在代码块后面。但是,我想更明确地表明,第二个块是第一个块的结果,比如一个简单的标签。

如果我修改上面的,像这样:

#+begin_src cpp -n :includes <iostream> :exports both
std::cout << "Hello there";
#+end_src

Output:

#+RESULTS:
: Hello there

然后出现标签“输出:”,但结果块出现两次——一次在标签之前,一次在标签之后。更糟糕的是,如果我在 orgmode (C-c C-c) 中运行代码,则会在文本标签“输出:”之前放置第二个结果块。我怀疑这也是出口的情况。

我还注意到,当导出为 html 时,结果块被放置在 example 类的 pre 标记中。我想我可以用类似的东西修改css:

pre.example::before { content: "Output:"; }

但不幸的是,这会将文本放在pre 块内,我无法添加任何换行符。

是否有任何简单的方法可以在 orgmode 本身或通过 css 中将文本标签添加到结果块?如果可能,我想避免使用 javascript。

【问题讨论】:

    标签: html css org-mode


    【解决方案1】:

    这应该适用于最近的组织:

        #+name: foo
        #+begin_src cpp -n :includes <iostream> :exports both
        std::cout << "Hello there";
        #+end_src
    
        Output:
    
        #+RESULTS: foo
        : Hello there
    

    【讨论】:

      【解决方案2】:

      您可以像这样使用派生的后端:

      (defun my-results (fixed-width contents info)
        "Transform a results block to make it more visible."
        (let ((results (org-element-property :results fixed-width))
          (format (elt (plist-get info :back-end) 2))
          (value (org-element-property :value fixed-width)))
          (cond 
           ((eq 'html format)
            (format "<pre>Output:<br> %s</pre>" value)))))
      
      
      (org-export-define-derived-backend 'my-html 'html
        :translate-alist '((fixed-width . my-results)))
      
      (browse-url (org-export-to-file 'my-html (concat (file-name-base (buffer-file-name)) ".html")))
      

      【讨论】:

      • 如果我将此代码放在我的.emacs 文件中,我会收到错误消息Symbol's function definition is void: org-export-define-derived-backend 。我想可能是因为我的 org-mode 版本是8.2.something,所以我升级到了8.3.4。升级后我仍然收到此错误。
      • 应该在ox.el中定义。也许在该代码上方添加(需要'ox)。
      • 这不是您需要添加它的地方。你需要把它放在你的 .emacs 文件中。
      • 对不起,我误解了你所说的。如果我事先使用(require 'ox) 将您的代码添加到我的.emacs,我会收到Wrong argument type, stringp nil 的错误
      猜你喜欢
      • 1970-01-01
      • 2012-07-09
      • 1970-01-01
      • 2014-02-15
      • 2021-07-23
      • 1970-01-01
      • 2011-01-25
      • 2019-11-09
      • 2018-01-20
      相关资源
      最近更新 更多