9

I have been working on a document that I want to export to both HTML and latex; while working on the document I noticed that using latex \label{} and \ref{} tags works properly for latex export, but these are shown as plain tex in the HTML export. Similarly if I use Org links for references inside the document (for example a link to an Org header ) these are translated as links in the HTML export, but the latex export only shows a different typografy (\texttt{}), but I don't get any hyperlink characteristic.

Is there a way to the same behavior for both exports, either latex directives to translate into html links or Org links as in-file references for the latex export?

For an example of what I'm dealing with please check the following snippet.

#+OPTIONS: H4 num:3 toc:nil \n:nil @:t ::t |:t ^:nil -_t f:t *:t <:t
#+OPTIONS: TeX:t LaTeX:t skip:nil d:nil todo:t pri:nil tags:not-in-toc
#+OPTIONS: author:t email:nil creator:nil timestamp:nil
#+LATEX_HEADER: \usepackage{geometry}
#+LATEX_HEADER: \hypersetup{colorlinks, citecolor=black, filecolor=black, linkcolor=black, urlcolor=blue}


* Section name
\label{sec:section_name} (not working properly in html export)
some text
#+LABEL: fig:image
[[img][file:image]]
some reference to figure \ref{fig:image} =link work in latex but not in html=
* other section
reference to [[* Section name][link work in html, but not latex export]],but if I use the reference \nameref{sec:section_name} will not work in html, but it will in latex export

PS: I'm aware that I may try to use pandoc, but I have never used it and I don't know if I will get the desired result, and I'd rather not use add additional tools to the toolchain.

Thanks in advance

-- Edit, Added org-mode fragment as example

2
  • 4
    Can you give an example of your Org code? The second option you describe should actually do what you want. I.e. for example, [[Heading title]] should generate an anchor link in HTML and a reference in LaTeX file. If you want to style the links, you can use #+LATEX_HEADER: \hypersetup{urlcolor=blue} and #+LATEX_HEADER: \hypersetup{colorlinks,urlcolor=blue} (and see ftp.ntua.gr/mirror/ctan/macros/latex/contrib/hyperref/doc/… for more options).
    – wvxvw
    Commented May 12, 2015 at 4:49
  • 1
    Note that \nameref{sec:section_name} is absolutely not stable! E.g. in the current Org version, labels are translated to something like sec:orgheading1 by default unless org-latex-prefer-user-labels is non-nil.
    – rasmus
    Commented May 13, 2015 at 9:22

1 Answer 1

10

A couple of things..

  1. The syntax for figure/table labels is #+NAME:. You also need to use #+CAPTION: if you want to cross-reference them.
  2. You do not need to use the \label syntax; it's much simpler in org-mode.
    • To reference the section Section name (from your example), you use [[Section name]].
    • To reference that figure in your example, you use [[fig:image]].
  3. You need to use the #+LaTeX: prefix for LaTeX-only code.

Working MWE

* Section name
some text
#+CAPTION: Fig Caption
#+NAME: fig:image
[[file:img]]
some reference to figure [[fig:image]] =link will work in both html and latex=
* other section
reference to Section [[Section name]] =link will work in both html and latex=

Using CUSTOM_ID to refer to sections

Thanks to the tip from @rasmus in the comments, this is a better way to link to sections as it decouples the section name from the link name.

* Introduction
:PROPERTIES:
:CUSTOM_ID: sec:intro
:END:
some text
* Conclusion
This links to Section [[#sec:intro]] and this link will work in both html and latex. 
*It is important to prefix the custom ID with the =#= character.*

One more way to refer to sections (double angular brackets)

* Introduction
<<sec:intro>>
some text
* Conclusion
This links to Section [[sec:intro]] and this link will work in both html and latex.
*You do not prefix the link name with the =#= character here.*

References

  1. Figures in org-mode
  2. Internal Links
  3. Quoting LaTeX
7
  • In addition you can use CUSTOM_ID to refer to headings. I would recommend this as it makes references to headings orthogonal to the the displayed name.
    – rasmus
    Commented May 13, 2015 at 9:18
  • @rasmus CUSTOM_ID didn't work for me for latex/pdf exports (labels were auto generated: example sec:5-1), but worked fine for html exports. Commented May 13, 2015 at 23:45
  • 2
    you use custom_id like [[#my-custom-id]]. To force custom-id to be used as labels you can use org-latex-prefer-user-labels in the git ver version of org (it's called something else in 8.2).
    – rasmus
    Commented May 14, 2015 at 12:32
  • @rasmus Thanks. I'll try that out later today. But [[my-custom-id]] (without the #) worked, but only for html exports. May be I just need to set that var. Commented May 14, 2015 at 12:56
  • 1
    @rasmus I have updated the solution. Commented May 14, 2015 at 14:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.