4

I want to check if the value of the parameter is negative. If the parameter is negative, I want to substract 0.5 and if it is positive, I want to add 0.5.

The best I could do is define new variable like this

\ifnum#1<0 \def\xa{#1-0.5} \else \def\xa{#1+0.5}\fi

but this does not work. Is this possible to do in plan tex/latex without external library?

EDIT: Since #1 is fortunately integer, this works \def\xa{#1.5}. However, what if I wanted to add 1.5?

Pygmalion
  • 6,387
  • 4
  • 34
  • 68

3 Answers3

3

Too long for a comment. As mentioned in the comment for the original question you only need to append .5 to \xa. For the extended question, you can work with \the\numexpr which allows you to add integers. (If you wanted to add non-integers you could work with dimensions but presumably it makes more sense to employ xfp in that case.) The macro \mytest refers to the original question, \anothertest to the case in which you want to add/subtract 1.5, and \generaltest allows you to shift by general integers, which is the optional parameter the default of which is 1.

\documentclass{article}
\newcommand{\mytest}[1]{\edef\xa{#1.5}}
\newcommand{\anothertest}[1]{\ifnum#1<0\relax
 \edef\xa{\the\numexpr#1-1}%
\else 
 \edef\xa{\the\numexpr#1+1}%
\fi
\edef\xa{\xa.5}}
\newcommand{\generaltest}[2][1]{\ifnum#2<0\relax
 \edef\xa{\the\numexpr#2-#1}%
\else 
 \edef\xa{\the\numexpr#2+#1}%
\fi
\edef\xa{\xa.5}}
\begin{document}
\mytest{2}\xa

\mytest{-4}\xa

\anothertest{2}\xa

\anothertest{-4}\xa

\generaltest[1]{2}\xa

\generaltest[1]{-4}\xa

\generaltest[2]{2}\xa

\generaltest[6]{-4}\xa

\end{document}

3

(Found it would be too long to fit for a comment so I post this one)

To add a general number possibly with decimals, you will nee

  1. append pt to each number
  2. use \the\dimexpr to do calculations, and get back a dimension
  3. define a macro to strip the trailing pt, e.g. \pgf@sys@tonumber in pgf and \strip@pt in latex2e.
% pgf, pgfsys.code.tex
{\catcode`\p=12\catcode`\t=12\gdef\Pgf@geT#1pt{#1}}

\def\pgf@sys@tonumber#1{\expandafter\Pgf@geT\the#1}

% latex2e, ltfssbas.dtx % bonus: this also simplify 1.0pt to 1, not 1.0 \begingroup \catcodeP=12 \catcodeT=12 \lowercase{ \def\x{\def\rem@pt##1.##2PT{##1\ifnum##2>\z@.##2\fi}}} \expandafter\endgroup\x \def\strip@pt{\expandafter\rem@pt\the}

Combining all these,

\documentclass{article}
\makeatletter
\newcommand{\addnum}[2]{\expandafter\strip@pt\dimexpr#2pt+#1pt\relax}
\makeatother

\begin{document} \addnum{1}{1.3} \addnum{1}{-1.3} \end{document}

output

2.3 -0.3

muzimuzhi Z
  • 26,474
3

The issue is that mainly that you're working with decimals. The typical way of working with decimals in (La)TeX is to treat them like dimensions. So, instead of 2.5 (say), use 2.5pt. However, this approach is a bit cumbersome.

You can rely on the ternary operator of xfp that evaluates a condition and branches to evaluate either true or false, depending on the condition. Specifically, it has the following format:

condition ? true : false

Since \fpeval (for floating point operations) is expandable, you can use it to define some macro based on the calculation:

\documentclass{article}

\usepackage{xfp}

\newcommand{\addnum}[1]{% % <cond> ? <true> : <false> \edef\xa{\fpeval{#1 < 0 ? #1 - 0.5 : #1 + 0.5}}% }

\begin{document}

\addnum{2}$\xa$% 2 + 0.5 = 2.5

\addnum{-1}$\xa$% -1 - 0.5 = -1.5

\addnum{3.141}$\xa$% 3.141 + 0.5 = 3.641

\addnum{-2.718}$\xa$% -2.718 - 0.5 = -3.218

\end{document}

Werner
  • 603,163