GeneralHow to ceil or floor the value of length command

LaTeX specific issues not fitting into one of the other forums of this category.
Post Reply
fractal.generator
Posts: 66
Joined: Tue Oct 07, 2008 9:39 am

How to ceil or floor the value of length command

Post by fractal.generator »

dear all,

I want to print out the value of \textwidth, for example, in a certain precision.

Are there the ceil and floor functions available in latex to specify the precision?

Thank you in advance.

Here is my minimal code snippet:

Code: Select all

\documentclass{minimal}
\usepackage{printlen}
\uselengthunit{mm}

\begin{document}

The text width is \printlength{\textwidth}.

\end{document}

regards,

hayashi

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

Juanjo
Posts: 657
Joined: Sat Jan 27, 2007 12:46 am

How to ceil or floor the value of length command

Post by Juanjo »

fractal.generator wrote: I want to print out the value of \textwidth, for example, in a certain precision.
Are there the ceil and floor functions available in latex to specify the precision?
No, they aren't. The printlen package only provides the \rndprintlength command that just prints the integer part. Anyway, coming back to the code I gave you in other thread, with a slight change, you can round lengths to any decimal digit:

Code: Select all

\documentclass{article}

\usepackage{printlen}
\usepackage{fp}

\newlength{\TOarg} \newlength{\TOunit}
\makeatletter
\newcommand\TOop[2]{\setlength{\TOarg}{#2}%
   \FPdiv\TOres{\strip@pt\TOarg}{\strip@pt\TOunit}\FPround\TOres\TOres{#1}}
\makeatother
\newcommand{\TOspace}{\,} 
\newcommand\TOmm[2][2]{\setlength{\TOunit}{1mm}\TOop{#1}{#2}%
   \TOres\TOspace\ifmmode\mathrm{mm}\else mm\fi}
% If needed, define \TOin, \TOcm, \TOem... analogously, replacing mm by in, cm, em...

\begin{document}

\uselengthunit{mm}

The \verb|\textwidth| is \printlength{\textwidth}, that is, about \rndprintlength{\textwidth}.

The \verb|\textwidth| is \TOmm[5]{\textwidth}, that is, about \TOmm[0]{\textwidth}, 
or \TOmm[1]{\textwidth}, or \TOmm{\textwidth}, or \TOmm[3]{\textwidth}, or \TOmm[4]{\textwidth}.

\bigskip
\setlength{\unitlength}{1mm}

\noindent
Test of exactness:\\ 
\verb|\unitlength|= \printlength{\unitlength} \\
\verb|\unitlength|= \TOmm[5]{\unitlength}

\end{document}
If you replace \FPround by \FPtrunc, you get the floor function.
The CTAN lion is an artwork by Duane Bibby. Courtesy of www.ctan.org.
fractal.generator
Posts: 66
Joined: Tue Oct 07, 2008 9:39 am

Re: How to ceil or floor the value of length command

Post by fractal.generator »

Thanks Mr. Juanjo,

I works the way I want.


regards,

Hayashi
Post Reply