Page LayoutArithmetical operations in space definitions

Information and discussion about page layout specific issues (e.g. header and footer lines, page formats, page numbers).
Post Reply
theo moore
Posts: 72
Joined: Thu Jan 15, 2009 3:16 pm

Arithmetical operations in space definitions

Post by theo moore »

Hi, I define certain spaces in my document like so,

Code: Select all

\newcommand{\leftsize}{0.75\textwidth}
\newcommand{\cellsize}{0.15\textwidth}
I'd like to form a new value which is like,

Code: Select all

\newcommand{\prodsize}{\cellsize*\leftsize}
(Or perhaps I may want to subtract one from the other). Unfortunately, this does not seem to work, i.e. I have to manually code in,

Code: Select all

\newcommand{\prodsize}{0.1125\textwidth}
Is there a better solution?

Recommended reading 2024:

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

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

User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Arithmetical operations in space definitions

Post by gmedina »

Hi,

to do simple arithmetical operations on the arguments of commands, you could use the calc package.
1,1,2,3,5,8,13,21,34,55,89,144,233,...
theo moore
Posts: 72
Joined: Thu Jan 15, 2009 3:16 pm

Arithmetical operations in space definitions

Post by theo moore »

The calc package doesn't seem to like the command

Code: Select all

\newcommand{\prodsize}{\leftsize*\cellsize}
Any idea why? It does okay with things like,

Code: Select all

\newcommand{\prodsize}{\leftsize*\real{0.5}}
but not when I use two variables.
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Arithmetical operations in space definitions

Post by gmedina »

Can you please post your definitions for \leftsize and \cellsize?
1,1,2,3,5,8,13,21,34,55,89,144,233,...
theo moore
Posts: 72
Joined: Thu Jan 15, 2009 3:16 pm

Arithmetical operations in space definitions

Post by theo moore »

gmedina wrote:Can you please post your definitions for \leftsize and \cellsize?
They're in the first post.
User avatar
gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Arithmetical operations in space definitions

Post by gmedina »

Sorry about that.

Anyway, the problem comes from both \leftsize and \cellsize having a unit (pt, in this case). One possible solution is to use the \strip@pt LaTeX macro to get rid of the unit and then perform the multiplication:

Code: Select all

\documentclass{article}
\usepackage{calc}

\newlength\leftsize
\addtolength\leftsize{0.75\textwidth}
\newlength\cellsize
\addtolength\cellsize{0.15\textwidth}
\newlength\prodsize

\makeatletter
  \newcommand\mylen{\strip@pt\cellsize}
\makeatother

\addtolength\prodsize{\leftsize*\real{\mylen}}

\begin{document}

\the\leftsize

\the\cellsize

\the\prodsize

\end{document}
1,1,2,3,5,8,13,21,34,55,89,144,233,...
User avatar
Stefan Kottwitz
Site Admin
Posts: 10323
Joined: Mon Mar 10, 2008 9:44 pm

Arithmetical operations in space definitions

Post by Stefan Kottwitz »

Hi Theo,
  • I would not multiply macros, I recommend to use TeX lengths instead, because you are dealing with length values.
  • What do you expect as result of 0.75\textwidth*0.15\textwidth? That's not a length, it's an area.
Try this example:

Code: Select all

\documentclass[a4paper,10pt]{article}
\usepackage{calc}
\newlength{\leftsize}
\setlength{\leftsize}{0.75\textwidth}
\newlength{\cellsize}
\setlength{\cellsize}{0.15\textwidth}
\newlength{\addsize}
\setlength{\addsize}{\leftsize+\cellsize}
\newlength{\prodsize}
\newcommand*\factor{0.5}
\setlength{\prodsize}{\factor\cellsize}

\begin{document}
\rule{\leftsize}{1ex}

\rule{\cellsize}{1ex}

\rule{\addsize}{1ex}

\rule{\prodsize}{1ex}
\end{document}
Stefan
LaTeX.org admin
Post Reply