Re: insert automatically a reference to a section header and a link
On Wed, Nov 17, 2021 at 12:18 PM Uwe Brauer wrote: > > I type "As we have seen in section [[*Intro]]" (literally, I tend to > > not use any keybindings or function to insert the link). This (the > > "*Intro" inside double square brackets) is called an internal link to > > headlines and they are a default feature of Org: > > Yes, but, what do you do, if you have a 10 header and each has 20 > subheaders. How do you remember all these names? Seems like completion could help, either at point, or with completing-read? I've been playing a bit with something using the latter, and it's not alway so straightforward how to do this given the flexibility of org, but here's what it looks like using the Emacs 28 grouping support and Emacs 27 annotation support: https://raw.githubusercontent.com/bdarcus/oxr/main/images/oxr-insert.png Bruce
Re: insert automatically a reference to a section header and a link
Uwe Brauer writes: > Yes, but, what do you do, if you have a 10 header and each has 20 > subheaders. I avoid writing such complex documents for the moment. :) [Some custom functions] > Thanks but this did not work! Sorry. The code is meant as an inspiration, not to be used out-of-the box. I never tested the functions outside my highly adjusted default configuration. As I read in another post you seem to be a helm user. Therefore it might be a better idea to use helm helper functions. I assume, helm has also some function to jump around in Org files, with headings as targets. If you find such a function, try to copy and adapt (as I did with the similar `counsel-outline' function): I assume such a function will use some helper function to collect the candidates for completion and after the user selected a candidate there is code to jump to the selected destination. Just try to change this part so that instead of jumping, a link is inserted at the proper place (that's the part my function `sn/org-insert-internal-link-action' is responsible for). Maybe someone with more knowledge about helm might help you with a complete solution. -- Until the next mail..., Stefan.
Re: insert automatically a reference to a section header and a link
Juan Manuel Macías writes: > In Org 9.5 there is a new variable `org-latex-reference-command'. Thanks for the reminder! I once read about it, but forgot to use it after upgrading to 9.5. :) -- Until the next mail..., Stefan.
Re: insert automatically a reference to a section header and a link
Stefan Nobis writes: > #+begin_src emacs-lisp > (defun sn/ox-latex-filter-special-ref-vref (text backend info) > (when (org-export-derived-backend-p backend 'latex) > (replace-regexp-in-string "ref{" "vref{" text))) > > (add-to-list 'org-export-filter-link-functions > #'sn/ox-latex-filter-special-ref-vref) > #+end_src In Org 9.5 there is a new variable `org-latex-reference-command'. You can do something like: (setq org-latex-reference-command "\\vref{%s}") Best regards, Juan Manuel
Re: insert automatically a reference to a section header and a link
>>> "ESF" == Eric S Fraga writes: > On Wednesday, 17 Nov 2021 at 17:00, Uwe Brauer wrote: >> While >> >> ** That <> > Well, you could write > ** That > <> Right, good idea > to get what you like in terms of LaTeX export. > I tend to put the label on the headline so that I can see it when I am > in outline view. Personal taste. That is also true. In any case, thanks! smime.p7s Description: S/MIME cryptographic signature
Re: insert automatically a reference to a section header and a link
>>> "SN" == Stefan Nobis writes: > Uwe Brauer writes: >> I am not following you. You insert a header as in >> * Intro >> Now you want to refer to it with a link >> As we have seen in section ... >> So what precisely are you typing? > I type "As we have seen in section [[*Intro]]" (literally, I tend to > not use any keybindings or function to insert the link). This (the > "*Intro" inside double square brackets) is called an internal link to > headlines and they are a default feature of Org: Yes, but, what do you do, if you have a 10 header and each has 20 subheaders. How do you remember all these names? Besides your approach is not automatic. > https://orgmode.org/manual/Internal-Links.html#Internal-Links > If you need internal links to headings more often and do not want to > type the whole heading text manually, you can use functions like this > (inspired by `worf-goto' and `counsel-outline'; if you do not use > ivy/swiper/counsel, the code should at least give the basic idea): > #+begin_src emacs-lisp > (defun sn/org-insert-internal-link () > "Use ivy to insert a link to a heading in the current `org-mode' document." > (interactive) > (let ((settings (cdr (assq major-mode counsel-outline-settings > (ivy-read "Heading: " (counsel-outline-candidates settings) > :action 'sn/org-insert-internal-link-action))) > (defun sn/org-insert-internal-link-action (x) > "Insert link for `sn/worf-insert-internal-link'" > (save-excursion > (goto-char (cdr x)) > (call-interactively 'org-store-link)) > (org-insert-last-stored-link 1) > (delete-char -1)) > #+end_src Thanks but this did not work! I copied, In a separate file: #+begin_src (require 'counsel) (require 'worf) (defun sn/org-insert-internal-link () "Use ivy to insert a link to a heading in the current `org-mode' document." (interactive) (let ((settings (cdr (assq major-mode counsel-outline-settings (ivy-read "Heading: " (counsel-outline-candidates settings) :action 'sn/org-insert-internal-link-action))) (defun sn/org-insert-internal-link-action (x) "Insert link for `sn/worf-insert-internal-link'" (save-excursion (goto-char (cdr x)) (call-interactively 'org-store-link)) (org-insert-last-stored-link 1) (delete-char -1)) #+end_src When I loaded the file I obtained: ivy-configure: Keyword argument :init-fn not one of (:initial-input :height :occur :update-fn :unwind-fn :index-fn :sort-fn :format-fn :display-transformer-fn :more-chars :grep-p :exit-codes) When I tried to install worf, I obtained #+begin_src Compiling file /home/oub/.emacs.d/elpa/worf-20211014.1207/elpa.el at Wed Nov 17 17:59:27 2021 Entering directory ‘/home/oub/.emacs.d/elpa/worf-20211014.1207/’ Compiling file /home/oub/.emacs.d/elpa/worf-20211014.1207/worf.el at Wed Nov 17 17:59:27 2021 worf.el:143:1: Error: Keyword argument :init-fn not one of (:initial-input :height :occur :update-fn :unwind-fn :index-fn :sort-fn :format-fn :display-transformer-fn :more-chars :grep-p :exit-codes) #+end_src When I called your first function I obtained #+begin_src Debugger entered--Lisp error: (void-variable counsel-outline-settings) (assq major-mode counsel-outline-settings) (cdr (assq major-mode counsel-outline-settings)) (let ((settings (cdr (assq major-mode counsel-outline-settings (ivy-read "Heading: " (counsel-outline-candidates settings) :action 'sn/org-insert-internal-link-action)) sn/org-insert-internal-link() funcall-interactively(sn/org-insert-internal-link) call-interactively(sn/org-insert-internal-link record nil) command-execute(sn/org-insert-internal-link record) execute-extended-command(nil "sn/org-insert-internal-link" "sn/or") funcall-interactively(execute-extended-command nil "sn/org-insert-internal-link" "sn/or") call-interactively(execute-extended-command nil nil) command-execute(execute-extended-command) #+end_src So something is not right here! Uwe smime.p7s Description: S/MIME cryptographic signature
Re: insert automatically a reference to a section header and a link
>>> "ESF" == Eric S Fraga writes: > On Wednesday, 17 Nov 2021 at 16:55, Uwe Brauer wrote: > "SN" == Stefan Nobis writes: >>> As seen in [[*Intro]] there is not much to say. >> >> I am not following you. You insert a header as in >> >> * Intro >> >> >> Now you want to refer to it with a link > The point is that the link uses the heading text itself (see the *in the > link) to automatically find the heading. No need to put an explicit > label on the heading. I am sorry, I still don't get this. What I want is to insert, as convenient as possible a link to a headings. I usually use org-ref-helm-insert-ref-link for this. This function however needs a label of sorts. So how is your workflow? Suppose you have 20 headers and subheaders. How to you generate a link to subheader 6 of header 3 without a link? smime.p7s Description: S/MIME cryptographic signature
Re: insert automatically a reference to a section header and a link
On Wednesday, 17 Nov 2021 at 17:00, Uwe Brauer wrote: > While > > ** That <> Well, you could write --8<---cut here---start->8--- ** That <> --8<---cut here---end--->8--- to get what you like in terms of LaTeX export. I tend to put the label on the headline so that I can see it when I am in outline view. Personal taste. -- : Eric S Fraga, with org release_9.5-230-g2bbac4 in Emacs 29.0.50 : Latest paper written in org: https://arxiv.org/abs/2106.05096
Re: insert automatically a reference to a section header and a link
Uwe Brauer writes: > I am not following you. You insert a header as in > * Intro > Now you want to refer to it with a link > As we have seen in section ... > So what precisely are you typing? I type "As we have seen in section [[*Intro]]" (literally, I tend to not use any keybindings or function to insert the link). This (the "*Intro" inside double square brackets) is called an internal link to headlines and they are a default feature of Org: https://orgmode.org/manual/Internal-Links.html#Internal-Links BTW: I mostly export to PDF via LaTeX and usually use the combination of cleveref with varioref. Therefore I prefer to type "As we have seen in [[*Intro]]" (leaving out "section") and let Org generate a "\vref{...}" reference. To achive this, I need little bit of extra setup (and do not forget to add the necessary usepackage commands either locally in the Org file or add it to org-latex-default-packages-alist): #+begin_src emacs-lisp (defun sn/ox-latex-filter-special-ref-vref (text backend info) (when (org-export-derived-backend-p backend 'latex) (replace-regexp-in-string "ref{" "vref{" text))) (add-to-list 'org-export-filter-link-functions #'sn/ox-latex-filter-special-ref-vref) #+end_src If you need internal links to headings more often and do not want to type the whole heading text manually, you can use functions like this (inspired by `worf-goto' and `counsel-outline'; if you do not use ivy/swiper/counsel, the code should at least give the basic idea): #+begin_src emacs-lisp (defun sn/org-insert-internal-link () "Use ivy to insert a link to a heading in the current `org-mode' document." (interactive) (let ((settings (cdr (assq major-mode counsel-outline-settings (ivy-read "Heading: " (counsel-outline-candidates settings) :action 'sn/org-insert-internal-link-action))) (defun sn/org-insert-internal-link-action (x) "Insert link for `sn/worf-insert-internal-link'" (save-excursion (goto-char (cdr x)) (call-interactively 'org-store-link)) (org-insert-last-stored-link 1) (delete-char -1)) #+end_src -- Until the next mail..., Stefan.
Re: insert automatically a reference to a section header and a link
> Stefan Nobis writes: > Completely agree. I also tend to use `org-super-link' > (https://github.com/toshism/org-super-links). Interesting. This package is not in MELPA, but I will download and test it. > It can be run with helm-org-ql, so if there are a lot of headings in > the document, I just have to navigate through them using helm-org-ql, > and insert a link at point to the chosen candidate. And in the > destination header a back link is also inserted. > Best regards, > Juan Manuel smime.p7s Description: S/MIME cryptographic signature
Re: insert automatically a reference to a section header and a link
On Wednesday, 17 Nov 2021 at 16:55, Uwe Brauer wrote: "SN" == Stefan Nobis writes: >> As seen in [[*Intro]] there is not much to say. > > I am not following you. You insert a header as in > > * Intro > > > Now you want to refer to it with a link The point is that the link uses the heading text itself (see the *in the link) to automatically find the heading. No need to put an explicit label on the heading. -- : Eric S Fraga, with org release_9.5-230-g2bbac4 in Emacs 29.0.50 : Latest paper written in org: https://arxiv.org/abs/2106.05096
Re: insert automatically a reference to a section header and a link
>>> "ESF" == Eric S Fraga writes: > On Wednesday, 17 Nov 2021 at 14:58, Uwe Brauer wrote: >> Right, but how do you do this automatically? > John's given you an answer along the lines of what you wanted but I > would suggest that sometimes we over-complicate things? Hm not sure > LaTeX needs help because it's a lot more verbose. Org, however, has a > very simple markup ("It's all text" ;-)) and just typing what you want > is often quicker! Instead of key bindings to remember, I know I can > simply type > RET * <> heading RET > and I'm done... Well you should try auctex+reftex, it is just easier and (faster) if all this is done automatically. In auctex, you can also do this as you describe and I found that terrible and was very relived to find out about reftex, that insert labels automatically (and systematically) > Likewise, typing the reference is simply typing [[label]]. > And both bits above benefit from electric-pair-mode... > But I'm glad you found your solution in any case! There is just a small problem with this approach. When exported to latex ** Hello :PROPERTIES: :CUSTOM_ID: sec:wo :END: Is exported as \subsection{Hello} \label{sec:wo} While ** That <> Is exported as \subsection{That \label{sec:That}} \label{sec:org213d0b6} The first one is much nicer and more in line with the logic of latex, the second however is not bad (since it generates correct pdf files when compiled) I might think how to generalize John's approach. Uwe smime.p7s Description: S/MIME cryptographic signature
Re: insert automatically a reference to a section header and a link
>>> "SN" == Stefan Nobis writes: > Uwe Brauer writes: >> I wonder why this was not implemented or asked for. > Hmmm... for me, the default way to link to headings is just fine, I > seldom need more control over the generated labels: > #+begin_src org > ,* Intro > Lorem ipsum dolor sit amet... > ,* Another section > As seen in [[*Intro]] there is not much to say. > #+end_src I am not following you. You insert a header as in * Intro Now you want to refer to it with a link As we have seen in section ... So what precisely are you typing? All I can say is this to motivate this feature: I am a long time user of auctex+reftex, and the functionality to have labels added (in a systematic way) automatically is a *great* time saver if you want to add clickable references (links) So I am curious to see how you do that.
Re: insert automatically a reference to a section header and a link
Stefan Nobis writes: > Hmmm... for me, the default way to link to headings is just fine, I > seldom need more control over the generated labels: > > #+begin_src org > ,* Intro > > Lorem ipsum dolor sit amet... > > ,* Another section > > As seen in [[*Intro]] there is not much to say. > #+end_src Completely agree. I also tend to use `org-super-link' (https://github.com/toshism/org-super-links). It can be run with helm-org-ql, so if there are a lot of headings in the document, I just have to navigate through them using helm-org-ql, and insert a link at point to the chosen candidate. And in the destination header a back link is also inserted. Best regards, Juan Manuel
Re: insert automatically a reference to a section header and a link
Uwe Brauer writes: > I wonder why this was not implemented or asked for. Hmmm... for me, the default way to link to headings is just fine, I seldom need more control over the generated labels: #+begin_src org ,* Intro Lorem ipsum dolor sit amet... ,* Another section As seen in [[*Intro]] there is not much to say. #+end_src -- Until the next mail..., Stefan.
Re: insert automatically a reference to a section header and a link
On Wednesday, 17 Nov 2021 at 14:58, Uwe Brauer wrote: > Right, but how do you do this automatically? John's given you an answer along the lines of what you wanted but I would suggest that sometimes we over-complicate things? LaTeX needs help because it's a lot more verbose. Org, however, has a very simple markup ("It's all text" ;-)) and just typing what you want is often quicker! Instead of key bindings to remember, I know I can simply type RET * <> heading RET and I'm done... Likewise, typing the reference is simply typing [[label]]. And both bits above benefit from electric-pair-mode... But I'm glad you found your solution in any case! -- : Eric S Fraga, with org release_9.5-230-g2bbac4 in Emacs 29.0.50 : Latest paper written in org: https://arxiv.org/abs/2106.05096
Re: insert automatically a reference to a section header and a link
>>> "JK" == John Kitchin writes: > there is nothing like that that I know of. You can write your own, in org > (following Eric's use of radio targets) it might look like this. > (defun my-heading () > (interactive) > (let* ((title (string-trim (read-string "title: "))) > (tokens (split-string title " " t)) > (label (format "<>" > (string-join > (seq-take tokens (min 3 (length tokens))) > "-" > (insert (format "%s %s" label title > you could also insert the leading * if you prefer. Aha! Brilliant thanks! I will use (format "%s %s" title label) and think about inserting *. I wonder why this was not implemented or asked for. I googled but did not find anything. smime.p7s Description: S/MIME cryptographic signature
Re: insert automatically a reference to a section header and a link
there is nothing like that that I know of. You can write your own, in org (following Eric's use of radio targets) it might look like this. (defun my-heading () (interactive) (let* ((title (string-trim (read-string "title: "))) (tokens (split-string title " " t)) (label (format "<>" (string-join (seq-take tokens (min 3 (length tokens))) "-" (insert (format "%s %s" label title you could also insert the leading * if you prefer. John --- Professor John Kitchin (he/him/his) Doherty Hall A207F Department of Chemical Engineering Carnegie Mellon University Pittsburgh, PA 15213 412-268-7803 @johnkitchin http://kitchingroup.cheme.cmu.edu On Wed, Nov 17, 2021 at 8:59 AM Uwe Brauer wrote: > > >>> "ESF" == Eric S Fraga writes: > > > On Wednesday, 17 Nov 2021 at 08:36, Uwe Brauer wrote: > >> In auctex+reftex it is possible that when I insert a new section header > >> a reference label is inserted automatically: > > > I do the following usually: > > > * <> Introduction > > Right, but how do you do this automatically? > > Maybe I did not explain this clearly enough: in auctex+reftex, when I > fire up the command LaTeX-section, I am > > 1. Asked for the title > > 2. And then the title *and* a label is inserted automatically. (I >can configure what is inserted to a certain extend). Usually I >obtain \section{Alternativa de Fredholm} >\label{sec:altern-de-fredh} > > > I am looking for a similar functionality in org mode > > > In section [[introduction]], I show something. > > > I prefer visible labels instead of custom IDs although the latter works > > as well but you need to say [[id:introduction]]. > > > HTH, > > eric > > >
Re: insert automatically a reference to a section header and a link
>>> "ESF" == Eric S Fraga writes: > On Wednesday, 17 Nov 2021 at 08:36, Uwe Brauer wrote: >> In auctex+reftex it is possible that when I insert a new section header >> a reference label is inserted automatically: > I do the following usually: > * <> Introduction Right, but how do you do this automatically? Maybe I did not explain this clearly enough: in auctex+reftex, when I fire up the command LaTeX-section, I am 1. Asked for the title 2. And then the title *and* a label is inserted automatically. (I can configure what is inserted to a certain extend). Usually I obtain \section{Alternativa de Fredholm} \label{sec:altern-de-fredh} I am looking for a similar functionality in org mode > In section [[introduction]], I show something. > I prefer visible labels instead of custom IDs although the latter works > as well but you need to say [[id:introduction]]. > HTH, > eric
Re: insert automatically a reference to a section header and a link
On Wednesday, 17 Nov 2021 at 08:36, Uwe Brauer wrote: > In auctex+reftex it is possible that when I insert a new section header > a reference label is inserted automatically: I do the following usually: --8<---cut here---start->8--- * <> Introduction In section [[introduction]], I show something. --8<---cut here---end--->8--- I prefer visible labels instead of custom IDs although the latter works as well but you need to say [[id:introduction]]. HTH, eric -- : Eric S Fraga, with org release_9.5-230-g2bbac4 in Emacs 29.0.50 : Latest paper written in org: https://arxiv.org/abs/2106.05096
Re: insert automatically a reference to a section header and a link
>>> "UB" == Uwe Brauer writes: > Hi > In auctex+reftex it is possible that when I insert a new section header > a reference label is inserted automatically: > 1. Example >\subsection{Inverse problem} >\label{sec:one} > 2. And then using reftex-reference >I can insert \ref{sec:one} > So the equivalent of 1. in org mode would be to automatically insert > ** Inverse problem >:PROPERTIES: >:CUSTOM_ID: sec:one >:END: > Can this be done? > And then what would be the equivalent to 2? It is not org-insert-link, > as far as I can see. To answer at least one of my questions: org-ref-helm-insert-ref-link is the equivalent to reftex-reference. But I cannot see how to configure org, so that it automatically insert a section header link. smime.p7s Description: S/MIME cryptographic signature