emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [tip] Insert arbitrary LaTeX code at the beginning of any float environment
@ 2022-05-08 22:22 Juan Manuel Macías
  2022-05-09 12:47 ` Ihor Radchenko
  0 siblings, 1 reply; 6+ messages in thread
From: Juan Manuel Macías @ 2022-05-08 22:22 UTC (permalink / raw)
  To: orgmode

Hi all,

If we want to introduce arbitrary LaTeX code at the very beginning of a
float environment, apart from the usual tricks of putting the code in
:caption or :placement, this solution I describe here is more from the
LaTeX side. I thik its advantages are more control and consistency from
the point of view of LaTeX, and the possibility of introducing code of a
certain complexity.

Well, you have to write some LaTeX code for the preamble, but it's
really not much :)

We have to define a global command \myenvcode{<arbitrary code>} (if we
want to add the code to all document floats environments), and also a
'Myenvcode' environment, with the same argument. The etoolbox LaTeX
package provides a number of hooks: \AtBeginEnvironment,
\AtEndEnvironment, etc., but if we want our arbitrary code to be at the
very beginning of the environment, it's safer to use the \@floatboxreset
hook. So, the code that we would have to add to our preamble would be (I
prefer to define the command and environment using the xparse syntax):

#+NAME: preamble
#+begin_src latex :exports none
  \usepackage{xparse}

  \makeatletter
  \def\my@envcode{}
  \NewDocumentCommand{\myenvcode}{+m}{%
    \def\my@envcode{#1}}
  \g@addto@macro\@floatboxreset{\my@envcode}
  \makeatother

  \NewDocumentEnvironment{Myenvcode}{+m}{%
    \IfNoValueF{#1}{\myenvcode{#1}}}
  {\par}
#+end_src

#+begin_src latex :noweb yes :results raw
,#+LaTeX_HEADER: <<preamble>>
#+end_src

And here a few examples:

#+ATTR_LaTeX: :options {\centering\fbox{Code before a image}\par\vspace{1ex}}
#+begin_Myenvcode
#+CAPTION: This is a image
#+ATTR_LaTeX: :width .5\linewidth :placement [h]
[[file:example-image-a.jpg]]
#+end_Myenvcode

#+ATTR_LaTeX: :options {\captionsetup{font={color=red}}}
#+begin_Myenvcode
#+CAPTION: This is a image
#+ATTR_LaTeX: :width .5\linewidth :placement [h]
[[file:example-image-b.jpg]]
#+end_Myenvcode

#+latex: \myenvcode{{\centering\fbox{Code before a table}\par\vspace{1ex}}}
#+CAPTION: This is a table
#+ATTR_LaTeX: :placement [h] :booktabs t
| a | b | c | d | f |
|---+---+---+---+---|
| 1 | 2 | 3 | 4 | 5 |

#+latex: \myenvcode{}

Best regards,

Juan Manuel 


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

end of thread, other threads:[~2022-05-09 14:25 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-05-08 22:22 [tip] Insert arbitrary LaTeX code at the beginning of any float environment Juan Manuel Macías
2022-05-09 12:47 ` Ihor Radchenko
2022-05-09 13:15   ` Eric S Fraga
2022-05-09 13:51     ` Ihor Radchenko
2022-05-09 14:01   ` Juan Manuel Macías
2022-05-09 14:14     ` Eric S Fraga

Code repositories for project(s) associated with this 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).