# How to correctly convert size passed as argument to an environment?

I am trying and failing to adapt diagrams-latex.sty to do what I want. It takes optional parameters width and height that are passed on to an external script, but wants them as a dimensionless value that is semantically interpreted as bp. I would like to change the enviroment in a way that instead of

\begin{diagram}[width=100]
...
\end{diagram}


I can write:

\begin{diagram}[width=0.5\textwidth]
...
\end{diagram}


and the value is converted to bp behind the scenes and passed on to the external script as argument.

I managed to get a conversion function by adapting this answer, by defining

\usepackage{pgfmath}
\newcommand\tobp[1]{\pgfmathsetmacro\len{#1/1bp}\len{}}


and \tobp results in a dimensionless number, as I wanted to and it works when printing the resulting number into the document. But I can not call it in the parameter list like [width=\tobp{...}] and I also can not use it to wrap the argument in diagrams-latex.sty. I tried things like changing

\DeclareOptionX{width}[]{\def\dtt@width{-w #1}}


to

\DeclareOptionX{width}[]{\def\dtt@width{-w \tobp{#1}}}


but whatever I do, I get all kinds of error messages like

Incomplete \iffalse; all text was ignored after line 61.


How is what I want done properly?

"Minimal working example":

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{tikz}
\usepackage[backend=pgf, extension=pgf, outputdir=diagrams, input, cmdprefix={stack exec --}]{diagrams-latex}

% this seems to convert any unit to unitless number which is in bp
\usepackage{pgfmath}
\newcommand\tobp[1]{\pgfmathsetmacro\len{#1/1bp}\len{}}

\begin{document}

% this works
\tobp{\textwidth}
\tobp{10em}
\tobp{100pt}

% \begin{diagram}[width=0.5\textwidth] % <- what I ACTUALLY want to write
% \begin{diagram}[width=\tobp{0.5\textwidth}] % <- what I tried to do
\begin{diagram}[width=100]
dia = circle 1
\end{diagram}

\end{document}


Solution:

Add \RequirePackage{graphics}

and modify according line for width (and analogously height) to:

\DeclareOptionX{width}[]{\Gin@defaultbp\foo{#1}\def\dtt@width{-w \foo}}

• Welcome to TeX.SE! Could you please add a complete minimal working example, i.e. a short document that starts with \documentclass, ends with \end{document}, can be compiled and illustrates your point? – user121799 Nov 2 '18 at 21:39

the graphics package has a command that does exactly that:

100=100
.5\textwidth=171.85512
100bp=99.99976
100pt=99.62616


is the output from

\documentclass{article}
\usepackage{graphics}
\makeatletter

\Gin@defaultbp\foo{100}
\typeout{100=\foo}

\Gin@defaultbp\foo{.5\textwidth}
\typeout{.5\string\textwidth=\foo}

\Gin@defaultbp\foo{100bp}
\typeout{100bp=\foo}

\Gin@defaultbp\foo{100pt}
\typeout{100pt=\foo}

\stop