emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [tip] Export subfigures to LaTeX (and HTML)
@ 2020-12-25 16:02 Juan Manuel Macías
  2020-12-28 18:03 ` John Kitchin
  0 siblings, 1 reply; 3+ messages in thread
From: Juan Manuel Macías @ 2020-12-25 16:02 UTC (permalink / raw)
  To: orgmode

[-- Attachment #1: Type: text/plain, Size: 3199 bytes --]

Hi,

I have come up with a way to export subfigures to LaTeX (with the subfigure package) by
defining a new link type. The 'subcaption' of the subfigure would be the description of
the link. If we want to add parameters such as width, scale, etc., we can put them next
between the marks '>( ... )'

The code:

#+begin_src emacs-lisp
  (org-link-set-parameters
   "subfig"
   :follow (lambda (file) (find-file file))
   :face '(:foreground "chocolate" :weight bold :underline t)
   :display 'full
   :export (lambda (file desc backend)
	     (when (eq backend 'latex)
	       (if (string-match ">(\\(.+\\))" desc)
		   (concat "\\subfigure[" (replace-regexp-in-string "\s+>(.+)" "" desc) "]"
			   "{\\includegraphics"
			   "["
			   (match-string 1 desc)
			   "]"
			   "{"
			   file
			   "}}")
		 (format "\\subfigure[%s]{\\includegraphics{%s}}" desc file)))))
#+end_src

Example:

#+begin_src org
  ,#+CAPTION: Lorem impsum dolor
  ,#+ATTR_LaTeX: :options \centering
  ,#+begin_figure
  [[subfig:img1.jpg][Caption of img1 >(width=.3\textwidth)]]

  [[subfig:img2.jpg][Caption of img2 >(width=.3\textwidth)]]

  [[subfig:img3.jpg][Caption of img3 >(width=.6\textwidth)]]
  ,#+end_figure
#+end_src

Results:

#+begin_src latex
  \begin{figure}\centering
    \subfigure[Caption of img1]{\includegraphics[width=.3\textwidth]{img1.jpg}}

    \subfigure[Caption of img2]{\includegraphics[width=.3\textwidth]{img2.jpg}}

    \subfigure[Caption of img3]{\includegraphics[width=.6\textwidth]{img3.jpg}}
    \caption{Lorem impsum dolor}
  \end{figure}
#+end_src

If we want to export to HTML it would be something more tricky. In this case, the export
function could be like this (a width parameter would be enclosed between >{ ... }):

#+begin_src emacs-lisp
  (lambda (file desc backend)
    (cond
     ((eq backend 'latex)
      (if (string-match ">(\\(.+\\))" desc)
	  (concat "\\subfigure[" (replace-regexp-in-string "\s*>.+" "" desc) "]" "{\\includegraphics" "[" (match-string 1 desc) "]" "{"  file "}}")
	(format "\\subfigure[%s]{\\includegraphics{%s}}" (replace-regexp-in-string "\s*>.+" "" desc) file)))
     ((eq backend 'html)
      (if (string-match ">{\\(.+\\)}" desc)
	  (concat "<td><img src=\"" file "\" alt=\"" file "\"" " style=\"width:"
		  (match-string 1 desc)
		  "\""
		  "/><br>"
		  (replace-regexp-in-string "\s*&gt;.+" "" desc)
		  "</td>")
	(format "<td><img src=\"%s\" alt=\"%s\"/><br>%s</td>"
		file file
		(replace-regexp-in-string "\s*&gt;.+" "" desc))))))
#+end_src

Example:

#+begin_src org
  ,#+CAPTION: Lorem impsum dolor
  ,#+ATTR_LaTeX: :options \centering
  ,#+begin_figure
  @@html:<div class="org-center"><table style="margin-left:auto;margin-right:auto;"><tr>@@

  [[subfig:img1.jpg][Caption of img1 >(width=.3\textwidth) >{300px}]]

  [[subfig:img2.jpg][Caption of img2 >(width=.3\textwidth) >{300px}]]

  @@html:</tr></table><p> </p><table style="margin-left:auto;margin-right:auto;"><tr>@@

  [[subfig:img3.jpg][Caption of img3 >(width=.6\textwidth) >{600px}]]

  @@html:</tr></table><br>Lorem ipsum dolor</div>@@
  ,#+end_figure
#+end_src

As you can see, it is not the panacea, and you have to apply some direct format...

Happy holidays

Juan Manuel 

[-- Attachment #2: Type: text/html, Size: 0 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [tip] Export subfigures to LaTeX (and HTML)
  2020-12-25 16:02 [tip] Export subfigures to LaTeX (and HTML) Juan Manuel Macías
@ 2020-12-28 18:03 ` John Kitchin
  2020-12-29 15:00   ` Juan Manuel Macías
  0 siblings, 1 reply; 3+ messages in thread
From: John Kitchin @ 2020-12-28 18:03 UTC (permalink / raw)
  To: Juan Manuel Macías; +Cc: emacs-orgmode

This is an interesting use of links, in particular extending the
information in the description.

You might look at this
http://kitchingroup.cheme.cmu.edu/blog/2015/02/05/Extending-the-org-mode-link-syntax-with-attributes/
for another way to do that.

Check out this alternative approach all together that also uses a
special block.

http://kitchingroup.cheme.cmu.edu/blog/2016/01/17/Side-by-side-figures-in-org-mode-for-different-export-outputs/

I don't use either of these today, and they are old so who knows if they
still work, but they have some fun ideas in them.

Juan Manuel Macías <maciaschain@posteo.net> writes:

> Hi,
>
> I have come up with a way to export subfigures to LaTeX (with the subfigure package) by
> defining a new link type. The 'subcaption' of the subfigure would be the description of
> the link. If we want to add parameters such as width, scale, etc., we can put them next
> between the marks '>( ... )'
>
> The code:
>
> #+begin_src emacs-lisp
>   (org-link-set-parameters
>    "subfig"
>    :follow (lambda (file) (find-file file))
>    :face '(:foreground "chocolate" :weight bold :underline t)
>    :display 'full
>    :export (lambda (file desc backend)
> 	     (when (eq backend 'latex)
> 	       (if (string-match ">(\\(.+\\))" desc)
> 		   (concat "\\subfigure[" (replace-regexp-in-string "\s+>(.+)" "" desc) "]"
> 			   "{\\includegraphics"
> 			   "["
> 			   (match-string 1 desc)
> 			   "]"
> 			   "{"
> 			   file
> 			   "}}")
> 		 (format "\\subfigure[%s]{\\includegraphics{%s}}" desc file)))))
> #+end_src
>
> Example:
>
> #+begin_src org
>   ,#+CAPTION: Lorem impsum dolor
>   ,#+ATTR_LaTeX: :options \centering
>   ,#+begin_figure
>   [[subfig:img1.jpg][Caption of img1 >(width=.3\textwidth)]]
>
>   [[subfig:img2.jpg][Caption of img2 >(width=.3\textwidth)]]
>
>   [[subfig:img3.jpg][Caption of img3 >(width=.6\textwidth)]]
>   ,#+end_figure
> #+end_src
>
> Results:
>
> #+begin_src latex
>   \begin{figure}\centering
>     \subfigure[Caption of img1]{\includegraphics[width=.3\textwidth]{img1.jpg}}
>
>     \subfigure[Caption of img2]{\includegraphics[width=.3\textwidth]{img2.jpg}}
>
>     \subfigure[Caption of img3]{\includegraphics[width=.6\textwidth]{img3.jpg}}
>     \caption{Lorem impsum dolor}
>   \end{figure}
> #+end_src
>
> If we want to export to HTML it would be something more tricky. In this case, the export
> function could be like this (a width parameter would be enclosed between >{ ... }):
>
> #+begin_src emacs-lisp
>   (lambda (file desc backend)
>     (cond
>      ((eq backend 'latex)
>       (if (string-match ">(\\(.+\\))" desc)
> 	  (concat "\\subfigure[" (replace-regexp-in-string "\s*>.+" "" desc) "]" "{\\includegraphics" "[" (match-string 1 desc) "]" "{"  file "}}")
> 	(format "\\subfigure[%s]{\\includegraphics{%s}}" (replace-regexp-in-string "\s*>.+" "" desc) file)))
>      ((eq backend 'html)
>       (if (string-match "&gt;{\\(.+\\)}" desc)
> 	  (concat "<td><img src=\"" file "\" alt=\"" file "\"" " style=\"width:"
> 		  (match-string 1 desc)
> 		  "\""
> 		  "/><br>"
> 		  (replace-regexp-in-string "\s*&gt;.+" "" desc)
> 		  "</td>")
> 	(format "<td><img src=\"%s\" alt=\"%s\"/><br>%s</td>"
> 		file file
> 		(replace-regexp-in-string "\s*&gt;.+" "" desc))))))
> #+end_src
>
> Example:
>
> #+begin_src org
>   ,#+CAPTION: Lorem impsum dolor
>   ,#+ATTR_LaTeX: :options \centering
>   ,#+begin_figure
>   @@html:<div class="org-center"><table style="margin-left:auto;margin-right:auto;"><tr>@@
>
>   [[subfig:img1.jpg][Caption of img1 >(width=.3\textwidth) >{300px}]]
>
>   [[subfig:img2.jpg][Caption of img2 >(width=.3\textwidth) >{300px}]]
>
>   @@html:</tr></table><p> </p><table style="margin-left:auto;margin-right:auto;"><tr>@@
>
>   [[subfig:img3.jpg][Caption of img3 >(width=.6\textwidth) >{600px}]]
>
>   @@html:</tr></table><br>Lorem ipsum dolor</div>@@
>   ,#+end_figure
> #+end_src
>
> As you can see, it is not the panacea, and you have to apply some direct format...
>
> Happy holidays
>
> Juan Manuel


--
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


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [tip] Export subfigures to LaTeX (and HTML)
  2020-12-28 18:03 ` John Kitchin
@ 2020-12-29 15:00   ` Juan Manuel Macías
  0 siblings, 0 replies; 3+ messages in thread
From: Juan Manuel Macías @ 2020-12-29 15:00 UTC (permalink / raw)
  To: John Kitchin; +Cc: orgmode

Hello, John, 

Thank you very much for the two links.

John Kitchin <jkitchin@andrew.cmu.edu> writes:

> You might look at this
> http://kitchingroup.cheme.cmu.edu/blog/2015/02/05/Extending-the-org-mode-link-syntax-with-attributes/
> for another way to do that.

Your idea of adding attributes to Org links is very interesting and
productive. It also shows the great potential that Org links have.

> Check out this alternative approach all together that also uses a
> special block.
>
> http://kitchingroup.cheme.cmu.edu/blog/2016/01/17/Side-by-side-figures-in-org-mode-for-different-export-outputs/

I also find your approach very interesting. What I like the most is
giving LaTeX a "Lisp skin". It is more elegant and legible. I love
(La)TeX and its potential, but I find its code somewhat ugly, often too
verbose. By the way (offtopic), there is a LaTeX package called
lisp-on-TeX that includes a rudimentary Lisp interpreter:
https://www.ctan.org/pkg/lisp-on-tex. It's interesting, and you can do
some things with it, but I think it's an abandoned project. The TeX
ecosystem has moved to Lua with LuaTeX and LuaMetaTeX, but a LispTeX
would have been wonderful! ;-)

Regards,

Juan Manuel 


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2020-12-29 15:01 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-25 16:02 [tip] Export subfigures to LaTeX (and HTML) Juan Manuel Macías
2020-12-28 18:03 ` John Kitchin
2020-12-29 15:00   ` Juan Manuel Macías

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).