1

I want to substract and multiply with an optional parameter and a constant with cm as unit. Like this:

\documentclass{article}
\usepackage{changepage, xintexpr}

\newcommand{\test}[2][1]{
  \begin{adjustwidth}{(#1 -1)*0.8cm}{} %(#1 -1)*0.8cm
  %\begin{adjustwidth}{0.8}{}
    #2
  \end{adjustwidth}
  \begin{adjustwidth}{0.8cm}{}
    Something else I need \#1 to be 1 at default
  \end{adjustwidth}
}
\begin{document}
  \noindent line1
  \test{Sampletext}
\end{document}

However (#1 -1) * 0.8cm isn't working.
I've already achieved 0.8cm * #1 by using #1\dimexpr 0.8cm\relax
But I'm unable to get both operations working.
How can I do this?

PS.: Already looked at How to add, subtract, multiply, and divide in plain TeX? but couldn't figure it out...

1

You can use the e-LaTeX primitive \dimexpr. Note, however, that you need to do the multiplication in the correct order so instead of \dimexpr(#1-1)*0.8cm you need to use \dimexpr0.8cm*(#1-1); see formal syntax rules of \dimexpr \numexpr \glueexpr for more details.

Here is a full MWE:

\documentclass{article}
\usepackage{changepage, xintexpr}

\newcommand{\test}[2][1]{
  \begin{adjustwidth}{\the\dimexpr0.8cm*(#1-1)\relax}{} %(#1 -1)*0.8cm
    #2
  \end{adjustwidth}
  \begin{adjustwidth}{0.8cm}{}
    Something else I need \#1 to be 1 by default
  \end{adjustwidth}
}
\begin{document}
  \noindent line1
  \test{Sampletext}
  \test[6]{More sampletext}
\end{document}

This produces:

enter image description here

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.