emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [O] LaTeX export with section number, name and page in internal link


From: John Kitchin
Subject: Re: [O] LaTeX export with section number, name and page in internal links
Date: Tue, 08 Dec 2015 15:29:46 -0500
User-agent: mu4e 0.9.13; emacs 25.0.50.1

> I do something like this with custom link types.
Aha! I am not the only one ;)

>
> First of all, have a look at the variable
>
> org-latex-prefer-user-labels

Is this a new 8.3 variable? It doesn't seem to be in my 8.2.10 MELPA
version.

>
> if you haven't already.  Setting it will cause Org to use CUSTOM_ID
> properties to generate labels, so you don't need to manually insert your
> own.
>
> I use the following bit of Elisp to define some link types for referring
> to sections this way.  You could modify this to insert the LaTeX command
> you're interested in (as opposed to just \ref{}).  With your example
> above, you'd write something like

I like your idea. I think you could simplify it to just:

(org-add-link-type
 "sec"
 (lambda (path)
   (org-open-link-from-string (format "[[#%s]]" path)))
 (lambda (path desc format)
   (cond
    ((eq format 'latex)
     (format "%s~\\ref{%s}" (or desc "Section") path)))))

and refer to [[sec:section-1]] that looks like this below.

** some title
   :PROPERTIES:
   :CUSTOM_ID: section-1
   :END:
>
> #+BEGIN_EXAMPLE
> I want reference to Section 1.1 from here (See [[sec:section-1]]).
> #+END_EXAMPLE
>
> Here's the code:
> #+BEGIN_SRC elisp
> ;; Link types for targeting sections, tables, etc.
> ;; These assume that headlines with CUSTOM_ID defined will export using
> ;; that value as their \label keys.
> (defun org-find-headline-by-custom-id (prefix path)
>   "Find a headline in the current buffer by CUSTOM_ID value PREFIX:PATH."
>   (save-excursion
>     (goto-char (point-min))
>      (and
>       ; borrowed from org.el; there doesn't seem to be a function that 
> searches
>       ; for a headline with a specific property value
>       (re-search-forward
>        (concat "^[ \t]*:CUSTOM_ID:[ \t]+" prefix ":" path "[ \t]*$") nil t)
>       (setq pos (match-beginning 0))))
>    (if pos
>        (progn
>        (goto-char pos)
>        (org-back-to-heading t))
>      (message (format "Headline with CUSTOM_ID %s:%s not found." prefix 
> path))))
>
> (defun org-export-dissertation-link (prefix path desc format)
>   "Export a link to a dissertation section, etc.
>
> In LaTeX, the exported link will look like:
>   DESC~\\ref{PREFIX:PATH}
> "
>     (when (member format '(latex linguistics))
>       (format "%s~\\ref{%s:%s}" desc prefix path)))
>
> ; Sections:
> (org-add-link-type
>  "sec"
>  (lambda (path)
>    (org-find-headline-by-custom-id "sec" path))
>  (lambda (path desc format)
>    (org-export-dissertation-link "sec" path (or desc "Section") format)))
>
> ; etc. etc.
> #+END_SRC elisp
>
> Best,
> Richard
>
> OpenPGP Key ID: CF6FA646
> Fingerprint: 9969 43E1 CF6F A646
>
> (See http://www.ocf.berkeley.edu/~rwl/encryption.html for more information.)

--
Professor John Kitchin
Doherty Hall A207F
Department of Chemical Engineering
Carnegie Mellon University
Pittsburgh, PA 15213
412-268-7803
@johnkitchin
http://kitchingroup.cheme.cmu.edu



reply via email to

[Prev in Thread] Current Thread [Next in Thread]