CDSoft / pp

PP - Generic preprocessor (with pandoc in mind) - macros, literate programming, diagrams, scripts...

Home Page:http://cdelord.fr/pp

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

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

vittorioromeo opened this issue · comments

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:

  • Add an optional { ... } field to graph generating functions such as \dot and \twopi, called attribute field.
  • The attribute field, if present, must follow the (...) caption field.
  • The attribute field, if present, will generate a valid attribute list at the end of generated Pandoc image syntax.
    • It is sufficient to simply copy the whole attribute field (including the curly braces) after the path specification in the Pandoc image inclusion code.

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

I think the

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

syntax looks fine.

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

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

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