7

I would like to use LaTeX 3 to round up decimal numbers; I want something like the following: If I type

\RoundingUpFunction{2.7}

it should return

3

as output.

Unfortunately, I haven't taken the time to learn the LaTeX 3 syntax yet, so if someone will show me how to define such a function (or tell me what it is if it already exists), I will much appreciate it.

Update

If I try to use Marco Daniel's answer in the following way, it still doesn't work:

\documentclass{article}

\usepackage{pst-optic,pstricks-add}
\usepackage{xparse}

\newcommand*\Lens[1][]{%
  \psclip{%
    \psframe[linestyle=none](-\horizontal,-\vertical)(\horizontal,\vertical)
  }
  \rput(0,0){\lensSPH[#1]}
  \endpsclip
}

\ExplSyntaxOn
\NewDocumentCommand {\RoundUp} { m }
 {
  \fp_eval:n { round+(#1) }
 }
\ExplSyntaxOff

\begin{document}

\begin{figure}
\def\horizontal{1}
\def\vertical{1.7}
 \centering
  \begin{pspicture}[showgrid](-\RoundUp{\horizontal},-\RoundUp{\vertical})%
                             ( \RoundUp{\horizontal}, \RoundUp{\vertical})
   \Lens[
     lensType=CVG,
     lensWidth=1,
     lensColor=BleuVerre,
     drawing=false
   ]
  \end{pspicture}
\end{figure}

\end{document}

with the error message

! Undefined control sequence.
<argument> \LaTeX3 error: 
                           Unknown fp word a.
l.28 ...RoundUp{\horizontal}, \RoundUp{\vertical})

2 Answers 2

11

If you want to use LaTeX3 you can use ceil:

\documentclass{article}

\usepackage{xparse}
\ExplSyntaxOn
\NewDocumentCommand {\RoundingUpFunction} { m }
 {
  \fp_eval:n { ceil(#1) }
 }
\ExplSyntaxOff


\begin{document}
\RoundingUpFunction{1.7}%leads to 2

\RoundingUpFunction{1.2}%leads to 2

\RoundingUpFunction{1}%leads to 1

\end{document}

Related to your update use \DeclareExpandableDocumentCommand instead of \NewDocumentCommand

2
  • @SvendTveskæg: See update. Jun 3, 2013 at 14:02
  • 1
    With the most recent LaTeX3, round+ should be ceil IIRC. Jul 7, 2014 at 3:20
8

If you can bear not to use LaTeX 3 then you could use TikZ instead...

\documentclass{article}
\usepackage{pgfmath,pgfkeys}

\newcommand\RoundingUpFunction[1]{%
  \pgfmathparse{ceil(#1)}%
  \pgfmathprintnumber[int trunc]{\pgfmathresult}%
}

\begin{document}
\RoundingUpFunction{2.7}
\end{document}

3

5
  • This is good relativt to what I first asked for. This doesn't work for me however, so I'll edit my question. Jun 3, 2013 at 13:13
  • surely you meant "prepared not to use" rather than "happy not to use" :-) Jun 3, 2013 at 13:25
  • @DavidCarlisle Fixed :-) Jun 3, 2013 at 13:27
  • 3
    Minor detail: You only need pgfmath,pgfkeys (this is fixed so now, in CVS version, only pgfmath is enough). Also you can add int(ceil(#1)) so you can directly type \pgfmathresult
    – percusse
    Jun 3, 2013 at 13:50
  • @percusse Thanks, I've fixed my answer. I thought I didn't need all of TikZ, but when I used just pgfmath it didn't work. I hadn't realised I'd need pgfkeys too. Jun 3, 2013 at 14:07

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .