1

how can I make the sum of 2 variables in cm? Example: \y+ \n \x + \m

Thank in advance

Minimal coding:

\documentclass{article}
\usepackage[absolute,overlay]{textpos}
\usepackage{xcolor}

\newcommand*{\x}{0.5cm}
\newcommand*{\y}{2.0cm}
\newcommand*{\m}{1.5cm}
\newcommand*{\n}{2.5cm}

\begin{document}
\title{Texblock color}

\textblockcolour{cyan}
\begin{textblock*}{5cm}(2cm,10cm)
  123 

\end{textblock*}
\textblockcolour{red}
\begin{textblock*}{5cm}(\x,\y+\n)
  456 
\end{textblock*}

\textblockcolour{yellow}
\begin{textblock*}{5cm}(\x+\m,16cm)
  789
\end{textblock*}

\end{document}
Bernard
  • 271,350
latexforti
  • 2,091

2 Answers2

2

I suggest not defining one-letter command, but to use a more semantic approach.

\documentclass{article}
\usepackage[absolute,overlay]{textpos}
\usepackage{xcolor}

\ExplSyntaxOn \NewDocumentCommand{\definevar}{mm} { \tl_clear_new:c { l_forti_var_#1_tl } \tl_set:cn { l_forti_var_#1_tl } { #2 } } \NewExpandableDocumentCommand{\usevar}{m} { \tl_use:c { l_forti_var_#1_tl } } \ExplSyntaxOff

\definevar{x}{0.5cm} \definevar{y}{2.0cm} \definevar{m}{1.5cm} \definevar{n}{2.5cm}

\begin{document} \title{Texblock color}

\textblockcolour{cyan} \begin{textblock}{5cm}(2cm,10cm) 123 \end{textblock}

\textblockcolour{red} \begin{textblock}{5cm}(\usevar{x},\dimeval{\usevar{y}+\usevar{n}}) 456 \end{textblock}

\textblockcolour{yellow} \begin{textblock}{5cm}(\dimeval{\usevar{x}+\usevar{m}},16cm) 789 \end{textblock}

\end{document}

enter image description here

You can still go the \newcommand way, with the same syntax with \dimeval to perform the operation, so like

\begin{textblock*}{5cm}(\x,\dimeval{\y+\n})

if you prefer the style I don't recommend.

egreg
  • 1,121,712
0

Use calculator package.

\documentclass{article}
\usepackage{calculator}
\usepackage{amsmath}
\newcommand{\x}{0.5}
\newcommand{\y}{2}
\newcommand{\m}{1.5}
\newcommand{\n}{2.5}
\begin{document}
\ADD{\x}{\y}{\one}
\(\x\text{cm} + \y\text{cm} = \one\text{cm}\)

\ADD{\m}{\n}{\two} And (\m\text{cm} + \n\text{cm} = \two\text{cm}) \end{document}

Equation

  • 1
    This doesn't really answer the question. It's not only about making the computation but mostly about making it work in the argument of a {textblock*} environment. – campa Aug 18 '23 at 13:42