I recently discovered Latex and is using TexMaker to edit my documents, in one of my university courses we will be performing a number of tests on a hydraulic rig. Since the tests will be conducted by a different person than the one writing the test document, there is considerable room for mistakes.

I was thinking something along these lines of creating a command that matches the arguments for the variable to the corresponding value.

    \var{Pressure, Flow, Revolutions}{100 Bar, 25 L/min, 2000 rpm} 

    Generating the output: 
    Pressure 100 Bar 
    Flow 25 L/min
    Revolutions 2000 rpm

Is this even possible to create using \newcommand or are there any packages that could suit my needs?


A key-value interface seems the better choice:

  \borg_var:n {#1}

\keys_define:nn { borg/var }
  pressure    .tl_set:N = \l_borg_var_pressure_tl,
  flow        .tl_set:N = \l_borg_var_flow_tl,
  revolutions .tl_set:N = \l_borg_var_revolutions_tl,
  pressure    .initial:n = { missing value for pressure },
  flow        .initial:n = { missing value for pressure },
  revolutions .initial:n = { missing value for pressure },

\cs_new_protected:Npn \borg_var:n #1
  \keys_set:nn { borg/var } { #1 }
  Pressure ~ \tl_use:N \l_borg_var_pressure_tl \\
  Flow ~ \tl_use:N \l_borg_var_flow_tl \\
  Revolutions ~ \tl_use:N \l_borg_var_revolutions_tl



  pressure = \SI{100}{bar},
  flow = \SI{25}{L/min},
  revolutions= \SI{2000}{rpm}


The initial values could be changed into warning or even error messages, so as to ensure the values are all given.

Many refinements are possible, for instance if you need only the value and the units are fixed they could be printed automatically and allow for a syntax such as

  pressure = 100,
  flow = 25,
  revolutions = 2000

Just incorporate the calls to \SI in the macros with

\cs_new_protected:Npn \borg_var:n #1
  \keys_set:nn { borg/var } { #1 }
  Pressure ~ \SI{\l_borg_var_pressure_tl}{bar} \\
  Flow ~ \SI{\l_borg_var_flow_tl}{L/min} \\
  Revolutions ~ \SI{\l_borg_var_revolutions_tl}{rpm}

instead of the code above.

  • Much obliged, this is exactly what I was looking for. Easy to use, easy to modify and less prone to errors from other users. Thanks. – Charles Borg Oct 14 '13 at 9:13

Maybe an easier way?

\def\variable#1,#2,#3;#4,#5,#6;{#1\ #4\\ #2\ #5\\ #3\ #6\\}

with usage

\variable Pressure, Flow, Revolutions;100 Bar, 25 L/min, 2000 rpm;

Here ,'s and ;'s separate arguments.

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.