CDSoft / pp

PP - Generic preprocessor (with pandoc in mind) - macros, literate programming, diagrams, scripts...
http://cdelord.fr/pp
GNU General Public License v3.0
253 stars 21 forks source link

Generated figure attributes (width, height, id and class) #2

Closed vittorioromeo closed 8 years ago

vittorioromeo commented 8 years ago

Problem

I have come across a situation where I'd like to resize the figures generated by pp.

As far I can see, pp generates Pandoc image syntax after generating the figures.

I think Pandoc image syntax is limited, as it's impossible to choose figure alignment and scale.

Proposed solution

Pandoc supports width and height attributes for images. Look for the link_attributes extension in the docs here.

For example, the following Pandoc code:

![Caption](path/image.png){#some_id width=20%}

Generates the following LaTeX code:

\begin{figure}[htbp]
\centering
\includegraphics[width=0.20000\textwidth]{path/image.png}
\caption{Caption}\label{some_id}
\end{figure}

You can try this online, and play around with other outputs.

I propose extending pp's syntax as such:

\twopi(path/image.png)(Caption){#some_id width=20%}
\~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
digraph {
    O -> A
    O -> B
}
\~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The above example will generate the following Pandoc syntax:

![Caption](path/image.png){#some_id width=20%}

In short, what I propose is:

CDSoft commented 8 years ago

Currently, macro parameters must have the same delimiters, we can not mix (...) and {...}.

This constraint was added to help writing macros followed by expressions that could look like arguments (e.g. \macro(arg1)...(argn) [image.png](...) ==> [image.png] is not considered as a argument of macro)

With this syntax, the caption and this new argument would be optional which may be ambiguous in some cases (when only one is given).

Do you think:

\twopi(path/image.png)(Caption){#some_id width=20%}

could be replaced with:

\twopi(path/image.png{#some_id width=20%})(Caption)

Attributes related to the image would be grouped together in the first argument.

Another quick and dirty workaround would be to write:

\dot(image)(caption)
~~~~~~~~~
....
~~~~~~~~~{#some_id width=20%}

But I admit it looks bad ;-)

You can also use spaces and new lines (no blank line) to visually separate arguments:

\twopi(path/image.png{#some_id width=20%})
      (Caption)
~~~~~~~~~~~~~~~~~
....
~~~~~~~~~~~~~~~~~
vittorioromeo commented 8 years ago

I think the

\twopi(path/image.png{#some_id width=20%})
      (Caption)

syntax looks fine.

CDSoft commented 8 years ago

Image attributes can now be added with this syntax (spaces around brackets are accepted) :

\twopi(path/image { #some_id width=20% } )
      (Caption)
~~~~~~~~~~~~~~~~~
....
~~~~~~~~~~~~~~~~~
vittorioromeo commented 8 years ago

Thank you for the quick feature implementation. Works perfectly in my use case!