0

currently improving an existing package ('ditaa', based on ditaa0_9.jar) to add support for svg (implemented in ditaa-0.11.0-standalone.jar) yet I'm facing strange macro definition/expansion.

Basically I want to control the behavior of the 'ditaa' binary by adding support of its command line arguments that way (summed up) :

\ProvidesPackage{ditaa}
\RequirePackage[patch]{kvoptions}

% Support for https://github.com/stathissideris/ditaa
\DeclareBoolOption{A}               % Turns anti-aliasing off (-A or --no-antialias)
\DeclareBoolOption{noantialias}

\ProcessKeyvalOptions*

\newenvironment{ditaa}[3][\columnwidth]{
        % OPTION A <-----
        \def\ditaaoptA{\ifthenelse{\boolean{ditaa@A}}{-A }{\ifthenelse{\boolean{ditaa@noantialias}}{--no-antialias }{}}}

        % OPTION B <-----
        \ifthenelse{\boolean{ditaa@A}}{\def\ditaaoptA{-A }}{\ifthenelse{\boolean{ditaa@noantialias}}{\def\ditaaoptA{--no-antialias }}{\def\ditaaoptA{}}}

        \VerbatimOut{\ditaafile}}
    {\endVerbatimOut
        \immediate\write18{ditaa.jar \ditaaoptA "\ditaafile" "\ditaadir/\ditaastem.png"}    % <----- TITI
        \begin{figure}[H]
            \vdots      % because I'm lazy
        \end{figure}
        \ditaaoptA      % <----- TOTO
    }

Later...

\usepackage[A]{ditaa}

Ok, so as said, when OPTION A is used, TOTO displays the right argument (-A) but TITI fails because what is executed is something of the like :

runsystem(ditaa.jar \let \begingroup \let \begingroup \NROrg@begingroup \begingroup \def \protect \let \reserved@d = *\def \catcode `\ =5 \par )...executed.

However when using OPTION B, it works in both cases, YET it requires defining the same macro 3 times (with an empty variant) which is cumbersome to maintain and error prone.

Is there a way to get OPTION A (that is more elegant) to work no matter what ?

Thanks.

4
  • ifthenelse is not expandable. Use a test from etoolbox or an expl3 test. Side remark: kvoptions-patch was not compatible with a current latex and will not be loaded if it detects a new latex. Using \usepackage[patch]{kvoptions} doesn't make sense anymore. Commented Nov 4, 2020 at 14:16
  • Do -A and --no-antialias do the same thing or are they different Commented Nov 4, 2020 at 14:22
  • They do the same thing (stupid unix variance of argument names) that's why I only "activate" one (the first one found) if both are defined.
    – Kochise
    Commented Nov 4, 2020 at 14:34
  • @Kochise I made a complete rewrite (not sure I covered all points; tell me if I missed anything). Also, use @PhelypeOleinik (for example) to ping people, otherwise we won't get notified of your reply Commented Nov 4, 2020 at 15:01

2 Answers 2

3

As Ulrike said in the comment, \ifthenelse is not expandable, so it doesn't work inside a \write (that is, you must evaluate it first, set \ditaaoptA accordingly, and then pass it to \write).

Here's a reimplementation using an expl3 bool variable instead, and l3keys2e to parse the keys.

I made the no-antialias key a .bool_set:N key. The effect of no-antialias and no-antialias=true is the same, and you can also use no-antialias=false. I made A a .meta key that redirects to no-antialias (I wouldn't use it, though, and would only provide no-antialias), so you only have one boolean rather than having to check two (sanitise early and your code gets way simpler). I also defined keys for your other variables.

Then to call ditaa.jar you just need:

\sys_shell_now:x
  {
    ditaa.jar~ % executable
    \bool_if:NT \l__ditaa_no_antialias_bool { --no-antialias ~ } % --no-antialias if the bool is true
    "\l__ditaa_outfile_tl" ~ % whatever these other two are :)
    "\l__ditaa_path_tl/\l__ditaa_path_tl.png"
  }

I also made the optional argument to the environment be a list of keys that you can pass in a per-environment basis, rather than having them fixed by the load-time options.

\begin{filecontents}[overwrite]{ditaa.sty}

\RequirePackage{l3keys2e}
\RequirePackage{fancyvrb}

\ProvidesExplPackage{ditaa}{2020-11-04}
  {v1.0}{Interface to ditaa}

\keys_define:nn { ditaa }
  {
    , no-antialias .bool_set:N = \l__ditaa_no_antialias_bool
    , no-antialias .default:n = true
    , no-antialias .initial:n = false
    , A .meta:n = { no-antialias }
    %
    , output-file .tl_set:N = \l__ditaa_outfile_tl
    , output-file .initial:n = { \c_sys_jobname_str-ditaa }
    %
    , path .tl_set:N = \l__ditaa_path_tl
    , path .initial:n = { \c_sys_jobname_str-path } % <- insert reasonable defaults
    , stem .tl_set:N = \l__ditaa_stem_tl
    , stem .initial:n = { \c_sys_jobname_str-stem } % <- insert reasonable defaults
    %
  }

\ProcessKeysOptions { ditaa }

\NewDocumentEnvironment { ditaa } { o m m }
  {
    \group_begin:
      \IfValueT{#1} { \keys_set:nn { ditaa } {#1} }
      \VerbatimOut { \l__ditaa_outfile_tl }
  }
  {
      \endVerbatimOut
      % \exp_args:Nx \tl_show:n
      \sys_shell_now:x
        {
          ditaa.jar~
          \bool_if:NT \l__ditaa_no_antialias_bool { --no-antialias ~ }
          "\l__ditaa_outfile_tl" ~
          "\l__ditaa_path_tl/\l__ditaa_path_tl.png"
        }
    \group_end:
  }

\end{filecontents}

\documentclass{article}

\usepackage[A]{ditaa}

\begin{document}

\begin{ditaa}{a}{b}
\end{ditaa}

\begin{ditaa}[no-antialias=false]{a}{b}
\end{ditaa}

\end{document}
2
  • Looks cool, not used to this syntax, must get a better look at it :) Thanks, I'll post below my current version...
    – Kochise
    Commented Nov 4, 2020 at 15:21
  • @Kochise Well, you can alias some functions and use normal syntax, but I think (my opinion might be biased) that the code is more or less clear (once you assume that _ and : are allowed in macro names. \iow_shell_now:x is roughly the same as \immediate\write18, and \keys_set:nn parses a set of keys. The rest are mainly variables (that would have no _ in the usual syntax). Feel free to ask if you want Commented Nov 4, 2020 at 15:29
0

Here's my full version with things commented out because not functional/testing :

% 'ditaa' package
%
% (c) Hiroshi Ukai
%
%% This program can be redistributed and/or modified under the terms
%% of the LaTeX Project Public License Distributed from CTAN archives
%% in directory macros/latex/base/lppl.txt.
%
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{ditaa}
[2020/11/04 v0.11.0 LaTeX package for embedding ditaa style ascii art]

\RequirePackage{fancyvrb}
\RequirePackage{graphicx}
\RequirePackage{svg}
\RequirePackage{etoolbox}
\RequirePackage{xifthen}
\RequirePackage[debugshow,patch]{kvoptions}
%\RequirePackage[patch]{kvoptions}
%\RequirePackage{kvoptions-patch}

%\newcommand*{\ditaasetup}[1]{\setkeys{ditaa}{#1}}
\newcommand*{\ditaasetup}{\kvsetkeys{ditaa}}
\SetupKeyvalOptions{setkeys=\kvsetkeys}
%\SetupKeyvalOptions{family=ditaa, prefix=ditaa@, setkeys=\kvsetkeys}
%\SetupKeyvalOptions{}

% These options are defined at \usepackage[options]{ditaa} location, *not* at \begin[options]{ditaa}
% These options can be changed at "runtime" using the \ditaasetup{options} macro, though
\DeclareStringOption{imagepath}             % Path of the image
\DeclareStringOption[\columnwidth]{width}[\columnwidth]

% Support for https://github.com/stathissideris/ditaa
\DeclareBoolOption{A}                       % Turns anti-aliasing off
\DeclareBoolOption{noantialias}
\DeclareStringOption{b}[FFFFFF]             % The background colour of the image
\DeclareStringOption{background}[FFFFFF]
\DeclareBoolOption{d}                       % Renders the debug grid
\DeclareBoolOption{debug}
\DeclareBoolOption[true]{E}                 % Prevents the separation of common edges
\DeclareBoolOption{noseparation}
\DeclareStringOption{e}[utf-8]              % The encoding of the input file
\DeclareStringOption{encoding}[utf-8]
\DeclareBoolOption{h}                       % The input is an HTML file
\DeclareBoolOption{html}
\DeclareBoolOption{help}                    % Prints usage help
\DeclareBoolOption{o}                       % Image file is overwriten
\DeclareBoolOption{overwrite}
\DeclareBoolOption{r}                       % All corners to be rendered as round corners
\DeclareBoolOption{roundcorners}
\DeclareBoolOption{S}                       % Turns off the drop-shadow effect
\DeclareBoolOption{noshadows}
\DeclareStringOption{s}[1.0]                % Size of the rendered image
\DeclareStringOption{scale}[1.0]
\DeclareBoolOption{svg}                     % SVG image as destination file
\DeclareStringOption{svgfonturl}[]          % SVG font URL
\DeclareBoolOption{T}                       % Diagram rendered on a transparent background
\DeclareBoolOption{transparent}
\DeclareStringOption{t}[4]                  % Tabs interpreted as x spaces
\DeclareStringOption{tabs}[4]
\DeclareBoolOption{v}                       % Makes ditaa more verbose
\DeclareBoolOption{verbose}
\DeclareBoolOption{W}                       % Sides of parallelograms as fixed slope
\DeclareBoolOption{fixedslop}

\ProcessKeyvalOptions*
%\ProcessLocalKeyvalOptions*
%\ProcessKeyvalOptions{ditaa}

%@formatter:off (This line indicates IntelliJ that formatter should be off before this)
\newenvironment{ditaa}[3][\ditaa@width]
    {
        \def\ditaacaption{#2}
        \def\ditaastem{#3}
        \def\ditaadir{\ditaa@imagepath/ditaa}
        \def\ditaafile{\ditaadir/\ditaastem.ditaa}
        \def\ditaafigwidth{#1}
        % usepackage->program options ('-\/-' is de-ligaturing '--')

%       \def\ditaaoptA{\ifthenelse{\boolean{ditaa@A}}{-A}{\ifthenelse{\boolean{ditaa@noantialias}}{-\/-no-antialias}{}}}
%       \def\ditaaoptb{\ifdefstring{\ditaa@b}{}{\ifdefstring{\ditaa@background}{}{}{-\/-background \ditaa@background}}{-b \ditaa@b}}
%       \def\ditaaoptd{\ifthenelse{\boolean{ditaa@d}}{-d}{\ifthenelse{\boolean{ditaa@debug}}{-\/-debug}{}}}
%       \def\ditaaoptE{\ifthenelse{\boolean{ditaa@E}}{-E}{\ifthenelse{\boolean{ditaa@noseparation}}{-\/-no-separation}{}}}
%       \def\ditaaopte{\ifdefstring{\ditaa@e}{}{\ifdefstring{\ditaa@encoding}{}{}{-\/-encoding \ditaa@encoding}}{-b \ditaa@e}}
%       \def\ditaaopth{\ifthenelse{\boolean{ditaa@h}}{-h}{\ifthenelse{\boolean{ditaa@html}}{-\/-html}{}}}
%       \def\ditaaopthelp{\ifthenelse{\boolean{ditaa@help}}{-\/-help}{}}
%       \def\ditaaopto{\ifthenelse{\boolean{ditaa@o}}{-o}{\ifthenelse{\boolean{ditaa@overwrite}}{-\/-overwrite}{}}}
%       \def\ditaaoptr{\ifthenelse{\boolean{ditaa@r}}{-r}{\ifthenelse{\boolean{ditaa@roundcorners}}{-\/-round-corners}{}}}
%       \def\ditaaoptS{\ifthenelse{\boolean{ditaa@S}}{-S}{\ifthenelse{\boolean{ditaa@noshadows}}{-\/-no-shadows}{}}}
%       \def\ditaaopts{\ifdefstring{\ditaa@s}{}{\ifdefstring{\ditaa@scale}{}{}{-\/-scale \ditaa@scale}}{-s \ditaa@s}}
%       \def\ditaaoptsvg{\ifthenelse{\boolean{ditaa@svg}}{-\/-svg}{}}
%       \def\ditaaoptsvgext{\ifthenelse{\boolean{ditaa@svg}}{svg}{png}}
%       \def\ditaaoptsvgfonturl{\ifdefstring{\ditaa@svg-font-url}{}{}{-\/-svg-font-url \ditaa@svg-font-url}}
%       \def\ditaaoptT{\ifthenelse{\boolean{ditaa@T}}{-T}{\ifthenelse{\boolean{ditaa@transparent}}{-\/-transparent}{}}}
%       \def\ditaaoptt{\ifdefstring{\ditaa@t}{}{\ifdefstring{\ditaa@tabs}{}{}{-\/-tabs \ditaa@tabs}}{-t \ditaa@t}}
%       \def\ditaaoptv{\ifthenelse{\boolean{ditaa@v}}{-v}{\ifthenelse{\boolean{ditaa@verbose}}{-\/-verbose}{}}}
%       \def\ditaaoptW{\ifthenelse{\boolean{ditaa@W}}{-W}{\ifthenelse{\boolean{ditaa@fixedslop}}{-\/-fixed-slop}{}}}

        % \\def\\(ditaaopt[-\w]+)\{\\ifthenelse\{\\boolean\{(ditaa\@[-\w]+)\}\}\{([-\w]+)\}\{\\ifthenelse\{\\boolean\{(ditaa\@[-\w]+)\}\}\{-\\/-([-\w]+)\}\{\}\}\}
        % \\ifthenelse\{\\boolean\{\2\}\}\{\\def\\\1\{\3 \}\}\{\\ifthenelse\{\\boolean\{\4\}\}\{\\def\\\1\{-\\/-\5 \}\}\{\\\1\{\}\}\}

        % \\def\\(ditaaopt[-\w]+)\{\\ifdefstring\{\\(ditaa\@[-\w]+)\}\{\}\{\\ifdefstring\{\\(ditaa\@[-\w]+)\}\{\}\{\}\{-\\/-([-\w]+) \\\3\}\}\{-([-\w]+) \\\2\}\}
        % \\ifdefstring\{\\\2\}\{\}\{\\ifdefstring\{\\\3\}\{\}\{\\\1\{\}\}\{\\def\\\1\{-\\/-\4 \\\3 \}\}\}\{\\def\\\1\{-\5 \\\2 \}\}
        
        % \\def\\(ditaaopt[-\w]+)\{\\ifthenelse\{\\boolean\{(ditaa\@[-\w]+)\}\}\{-\\/-([-\w]+)\}\{\}\}
        % \\ifthenelse\{\\boolean\{\2\}\}\{\\def\\\1\{-\\/-\3 \}\}\{\\\1\{\}\}
        
        \ifthenelse{\boolean{ditaa@A}}{\def\ditaaoptA{-A }}{\ifthenelse{\boolean{ditaa@noantialias}}{\def\ditaaoptA{--no-antialias }}{\def\ditaaoptA{}}}
        \ifdefstring{\ditaa@b}{}{\ifdefstring{\ditaa@background}{}{\def\ditaaoptb{}}{\def\ditaaoptb{--background \ditaa@background }}}{\def\ditaaoptb{-b \ditaa@b }}
        \ifthenelse{\boolean{ditaa@d}}{\def\ditaaoptd{-d }}{\ifthenelse{\boolean{ditaa@debug}}{\def\ditaaoptd{--debug }}{\def\ditaaoptd{}}}
        \ifthenelse{\boolean{ditaa@E}}{\def\ditaaoptE{-E }}{\ifthenelse{\boolean{ditaa@noseparation}}{\def\ditaaoptE{--no-separation }}{\def\ditaaoptE{}}}
        \ifdefstring{\ditaa@e}{}{\ifdefstring{\ditaa@encoding}{}{\def\ditaaopte{}}{\def\ditaaopte{--encoding \ditaa@encoding }}}{\def\ditaaopte{-e \ditaa@e }}
        \ifthenelse{\boolean{ditaa@h}}{\def\ditaaopth{-h }}{\ifthenelse{\boolean{ditaa@html}}{\def\ditaaopth{--html }}{\def\ditaaopth{}}}
        \ifthenelse{\boolean{ditaa@help}}{\def\ditaaopthelp{--help }}{\def\ditaaopthelp{}}
        \ifthenelse{\boolean{ditaa@o}}{\def\ditaaopto{-o }}{\ifthenelse{\boolean{ditaa@overwrite}}{\def\ditaaopto{--overwrite }}{\def\ditaaopto{}}}
        \ifthenelse{\boolean{ditaa@r}}{\def\ditaaoptr{-r }}{\ifthenelse{\boolean{ditaa@roundcorners}}{\def\ditaaoptr{--round-corners }}{\def\ditaaoptr{}}}
        \ifthenelse{\boolean{ditaa@S}}{\def\ditaaoptS{-S }}{\ifthenelse{\boolean{ditaa@noshadows}}{\def\ditaaoptS{--no-shadows }}{\def\ditaaoptS{}}}
        \ifdefstring{\ditaa@s}{}{\ifdefstring{\ditaa@scale}{}{\def\ditaaopts{}}{\def\ditaaopts{--scale \ditaa@scale }}}{\def\ditaaopts{-s \ditaa@s }}
        \ifthenelse{\boolean{ditaa@svg}}{\def\ditaaoptsvg{--svg }}{\def\ditaaoptsvg{}}
        \ifthenelse{\boolean{ditaa@svg}}{\def\ditaaoptsvgext{svg}}{\def\ditaaoptsvgext{png}}
        \ifdefstring{\ditaa@svgfonturl}{}{\def\ditaaoptsvgfonturl{}}{\def\ditaaoptsvgfonturl{--svg-font-url "\ditaa@svgfonturl" }}
        \ifthenelse{\boolean{ditaa@T}}{\def\ditaaoptT{-T }}{\ifthenelse{\boolean{ditaa@transparent}}{\def\ditaaoptT{--transparent }}{\def\ditaaoptT{}}}
        \ifdefstring{\ditaa@t}{}{\ifdefstring{\ditaa@tabs}{}{\def\ditaaoptt{}}{\def\ditaaoptt{--tabs \ditaa@tabs }}}{\def\ditaaoptt{-t \ditaa@t }}
        \ifthenelse{\boolean{ditaa@v}}{\def\ditaaoptv{-v }}{\ifthenelse{\boolean{ditaa@verbose}}{\def\ditaaoptv{--verbose }}{\def\ditaaoptv{}}}
        \ifthenelse{\boolean{ditaa@W}}{\def\ditaaoptW{-W }}{\ifthenelse{\boolean{ditaa@fixedslop}}{\def\ditaaoptW{--fixed-slop }}{\def\ditaaoptW{}}}

        \VerbatimOut{\ditaafile}}
    {\endVerbatimOut
        \immediate\write18{ditaa.jar \ditaaoptA \ditaaoptb \ditaaoptd \ditaaoptE \ditaaopte \ditaaopth \ditaaopthelp \ditaaopto \ditaaoptr \ditaaoptS \ditaaopts \ditaaoptsvg \ditaaoptsvgfonturl \ditaaoptT \ditaaoptt \ditaaoptv \ditaaoptW "\ditaafile" "\ditaadir/\ditaastem.\ditaaoptsvgext"}
        \begin{figure}[H]
            \begin{center}
                \vspace{-1em}
                \ifditaa@svg
                    \includesvg[inkscapelatex=false, width=\ditaafigwidth]{\ditaadir/\ditaastem.svg}
                \else
                    \includegraphics[width=\ditaafigwidth]{\ditaadir/\ditaastem.png}
                \fi
                \vspace{-2em}
                \caption{\ditaacaption}
                \label{fig:\ditaastem}
                \vspace{-1.5em}
            \end{center}
        \end{figure}
    }
%@formatter:on (This line indicates IntelliJ that formatter should be off before this)
%--------------------------------------------------

\endinput
%%
%% End of file `ditaa.sty'.

Yeah, I also noticed that double-hyphens got 'eaten', so I read in another topic that '--' should be 'escaped' as '-\/-', yet while it works for OPTION A, it doesn't for OPTION B. Go figure...

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .