# Convert count into length

I want to decrease a length by a number (count). I tried some variations with \newlength{\temp} and \addtolength but I got problems there with the minus. I also tried variants with \dimexpr.

The following example is an absolute MWE. temp must be possibly negative. The mathematical operation must be a subtraction. Originally I had some \expandafters in there, but they are probably wrong and mess up the readability.

\documentclass[12pt,a4paper]{article}
\usepackage[utf8]{inputenc}
\usepackage[german]{babel}
\makeatletter

\begin{document}

\newcount\temp
\temp=-30

\def\haha#1{xyz \hspace{\advance\strip@pt#1 by -\temp pt} abc}

\haha{90pt}

\end{document}


The output should be

xyz-hspace60pt-abc

• This is as simple as \hspace{\dimexpr#1+\temp pt}. – Werner May 30 '16 at 21:03

## 1 Answer

The following achieves what you're after:

\documentclass{article}

\begin{document}

\newcounter{temp}
\setcounter{temp}{-30}

\newcommand{\haha}[1]{xyz-\hspace{\dimexpr#1+\value{temp} pt}-abc}

\haha{90pt}

xyz-\hspace{60pt}-abc% verification

\end{document}


I've used the LaTeX2e interface for counters, together with \value{<cntr>} to return a number of the counter <cntr>.

• Aaaah. I just found out (inspired by yours), that this works also: \def\haha#1{xyz \hspace{\dimexpr#1-\the\temp pt} abc} This is a hybrid of two of my approaches. And basically the same as yours. – MaestroGlanz May 30 '16 at 21:32
• @MaestroGlanz \the is redundant – egreg May 30 '16 at 21:35
• In my, lets say guess, I put the dimexpr in front of the wrong value of both. No wonder.... – MaestroGlanz May 30 '16 at 21:44