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

* Re: [tip] Insert arbitrary LaTeX code at the beginning of any float environment
  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 14:01   ` Juan Manuel Macías
  0 siblings, 2 replies; 6+ messages in thread
From: Ihor Radchenko @ 2022-05-09 12:47 UTC (permalink / raw)
  To: Juan Manuel Macías, Timothy; +Cc: orgmode

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

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

I'd be happy to see a built-in solution for this.
I feel that the ability to insert arbitrary LaTeX code near the
begin/end of environment would be generally a useful feature to have in
ox-latex. It could be done via #+attr_latex: :pre/:post

Moreover, it would be useful to be able to wrap the whole chunk into
custom environment:
#+attr_latex: :wrap [options]environmant

WDYT?

Best,
Ihor


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

* Re: [tip] Insert arbitrary LaTeX code at the beginning of any float environment
  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
  1 sibling, 1 reply; 6+ messages in thread
From: Eric S Fraga @ 2022-05-09 13:15 UTC (permalink / raw)
  To: Ihor Radchenko; +Cc: Juan Manuel Macías, Timothy, orgmode

On Monday,  9 May 2022 at 20:47, Ihor Radchenko wrote:
> Moreover, it would be useful to be able to wrap the whole chunk into
> custom environment:
> #+attr_latex: :wrap [options]environmant

This is already possible using special blocks?  I.e.

#+begin_environment
... figure stuff
#+end_environment

unless you mean something different.

-- 
: Eric S Fraga, with org release_9.5.3-478-g2a6f5c in Emacs 29.0.50


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

* Re: [tip] Insert arbitrary LaTeX code at the beginning of any float environment
  2022-05-09 13:15   ` Eric S Fraga
@ 2022-05-09 13:51     ` Ihor Radchenko
  0 siblings, 0 replies; 6+ messages in thread
From: Ihor Radchenko @ 2022-05-09 13:51 UTC (permalink / raw)
  To: Eric S Fraga; +Cc: Juan Manuel Macías, Timothy, orgmode

Eric S Fraga <e.fraga@ucl.ac.uk> writes:

> On Monday,  9 May 2022 at 20:47, Ihor Radchenko wrote:
>> Moreover, it would be useful to be able to wrap the whole chunk into
>> custom environment:
>> #+attr_latex: :wrap [options]environmant
>
> This is already possible using special blocks?  I.e.
>
> #+begin_environment
> ... figure stuff
> #+end_environment
>
> unless you mean something different.

Nope. I meant exactly this. Time to re-read the manual again :)

Best,
Ihor


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

* Re: [tip] Insert arbitrary LaTeX code at the beginning of any float environment
  2022-05-09 12:47 ` Ihor Radchenko
  2022-05-09 13:15   ` Eric S Fraga
@ 2022-05-09 14:01   ` Juan Manuel Macías
  2022-05-09 14:14     ` Eric S Fraga
  1 sibling, 1 reply; 6+ messages in thread
From: Juan Manuel Macías @ 2022-05-09 14:01 UTC (permalink / raw)
  To: Ihor Radchenko; +Cc: Eric S Fraga, orgmode

Ihor Radchenko writes:

> I'd be happy to see a built-in solution for this.
> I feel that the ability to insert arbitrary LaTeX code near the
> begin/end of environment would be generally a useful feature to have in
> ox-latex. It could be done via #+attr_latex: :pre/:post
>
> Moreover, it would be useful to be able to wrap the whole chunk into
> custom environment:
> #+attr_latex: :wrap [options]environmant
>
> WDYT?

I think they are both great ideas, especially the second one. Although
---as Eric says--- we can always use special blocks, a :wrap attribute
would drastically lighten the readability of the document, and could be
very useful in those complex constructions where we need to enclose a
table or a figure in another environment (for example, with the
threeparttable package and others like it).

Best regards,

Juan Manuel 


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

* Re: [tip] Insert arbitrary LaTeX code at the beginning of any float environment
  2022-05-09 14:01   ` Juan Manuel Macías
@ 2022-05-09 14:14     ` Eric S Fraga
  0 siblings, 0 replies; 6+ messages in thread
From: Eric S Fraga @ 2022-05-09 14:14 UTC (permalink / raw)
  To: Juan Manuel Macías; +Cc: Ihor Radchenko, orgmode

On Monday,  9 May 2022 at 14:01, Juan Manuel Macías wrote:
> Although ---as Eric says--- we can always use special blocks, a :wrap
> attribute would drastically lighten the readability of the document,

One advantage of special blocks is that they work across all
(some/most/?) export backends and are not tied to LaTeX.

-- 
: Eric S Fraga, with org release_9.5.3-478-g2a6f5c in Emacs 29.0.50


^ 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 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).