6

I'm busy with a document that change frequently. and special the used dimensions change. This result in a large chance that i miss a value when i change the dimensions. To tackle this problem i made a command with 3 variables (L,W,H). With the help of http://en.wikibooks.org/wiki/LaTeX/Macros i make the following command.

\newcommand{\dimsEN}[3][default value]{The dimensions ($\mathrm{L} \times \mathrm{W} \times
  \mathrm{H}$) of the room are $\mathrm{#1} \times \mathrm{#2} \times \mathrm{#3}$}

Now i cant figure out how to make 3 default values. so that i have 1 place to change all te values

Thanks

6

You want a key-value interface, so the order in which you set the values is irrelevant.

\documentclass{article}
\usepackage{keyval}

\makeatletter
\define@key{janbertdims}{l}{\def\janbert@l{#1}}
\define@key{janbertdims}{w}{\def\janbert@w{#1}}
\define@key{janbertdims}{h}{\def\janbert@h{#1}}
% initialize; change here to your preferred values
\setkeys{janbertdims}{
  l=1,        
  w=1,    
  h=1,
}

\newcommand\dimsEN[1][]{%
  \begingroup
  \setkeys{janbertdims}{#1}% the current values
  The dimensions ($\mathrm{L} \times \mathrm{W} \times \mathrm{H}$)
  of the room are
  $\janbert@l \times \janbert@w \times \janbert@h$%
  \endgroup
}
\makeatother

\begin{document}

\dimsEN

\dimsEN[h=3]

\dimsEN[h=4,w=2]

\dimsEN[w=3,l=5,h=2]

\end{document}

enter image description here

The same, but with expl3 syntax.

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\dimsEN}{ O{} }
 {
  \group_begin: % keep the changes to the keys local
  \janbert_dims_en:n { #1 }
  \group_end:
 }

\keys_define:nn { janbert/dims }
 {
  l .tl_set:N  = \l__janbert_dims_l_tl,
  w .tl_set:N  = \l__janbert_dims_w_tl,
  h .tl_set:N  = \l__janbert_dims_h_tl,
  %%% default values 
  l .initial:n = { 1 },
  w .initial:n = { 1 },
  h .initial:n = { 1 },
 }

\cs_new_protected:Npn \janbert_dims_en:n #1
 {
  \keys_set:nn { janbert/dims } { #1 }

  The ~ dimensions ~ ($\mathrm{L} \times \mathrm{W} \times \mathrm{H}$)
  of ~ the ~ room ~ are ~ 
  $\l__janbert_dims_l_tl \times \l__janbert_dims_w_tl \times \l__janbert_dims_h_tl$
}
\ExplSyntaxOff

\begin{document}

\dimsEN

\dimsEN[h=3]

\dimsEN[h=4,w=2]

\dimsEN[l=5,h=2,w=3]

\end{document}
3

All three can be set with \dimsEN<value>:

\documentclass[12pt]{article}
\makeatletter
\def\dimsEN{\@ifnextchar[\dimsEN@i\dimsEN@ii}
\def\dimsEN@i[#1]#2#3{%
  The dimensions ($\mathrm{L} \times \mathrm{W} \times
  \mathrm{H}$) of the room are $\mathrm{#1} \times \mathrm{#2} \times \mathrm{#3}$}
\def\dimsEN@ii{\@ifnextchar<\dimsEN@iii{\dimsEN@i[3]}}%%      3 is the default
\def\dimsEN@iii<#1>{\dimsEN@i[#1]{#1}{#1}}
\makeatother
\begin{document}

\dimsEN{1}{2}% default #1=3 

\dimsEN<5>% all are 5

\dimsEN[1]{2}{3}

\end{document}

enter image description here

Another possibility is to use a comma separated list:

\documentclass[12pt]{article}
\makeatletter
\def\dimsEN#1{\dimsEN@i#1\@nil}
\def\dimsEN@i#1,#2,#3\@nil{%
  \ifx\relax#1\relax \def\@argA{1}\else\def\@argA{#1}\fi%   the default is always 1
  \ifx\relax#2\relax \def\@argB{1}\else\def\@argB{#2}\fi%  
  \ifx\relax#3\relax \def\@argC{1}\else\def\@argC{#3}\fi% 
  The dimensions ($\mathrm{L} \times \mathrm{W} \times
  \mathrm{H}$) of the room are $\mathrm{\@argA} \times \mathrm{\@argB} \times \mathrm{\@argC}$}
  \makeatother
\begin{document}

\dimsEN{1,2,3} \par
\dimsEN{1,2,}  \par
\dimsEN{1,,}   \par
\dimsEN{,2,3}  \par
\dimsEN{,,3}   \par
\dimsEN{,,}  

\end{document}

enter image description here

It is also possible to check the existing of the commas but then the order is is important.

  • I have still fill 2 of the 3 dimensions in the command. I want 3 different default values. 1 value for the lenght; 1 value for the width and 1 value for the height. my problem is that i don't want to change the values on every place in the script. just 1 place at the beginning of the document! – Jan-Bert May 8 '14 at 12:04
  • see my edited answer – user2478 May 8 '14 at 12:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.