Text FormattingCustom fraction convertor produces strange space.

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
batonac
Posts: 12
Joined: Fri Feb 26, 2010 5:41 pm

Custom fraction convertor produces strange space.

Post by batonac »

I've been using ratexdb to create database reports using LaTex. I couldn't figure out how to convert decimal database input into LaTex fractions, so I custom coded a simple LaTex package which accepts decimal input (actually, the whole number & # of 64ths) and converts it into a LaTex fraction using the units package.

I felt pretty good about myself until I discovered that LaTex is erroneously inserting a huge amount of white space before and after my command. I've tried many different changes but can't seem to put my finger on the problem.

Here's my fracreduce package code:

Code: Select all

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{fracreduce}[04/06/2011 Converts database input (64ths) into latex fractions]

\RequirePackage{ifthen}
\RequirePackage{units}

% #1 is the whole number, #2 is the decimal, both fed from database
\newcommand{\fracreduce}[2]
{
  \ifthenelse{\equal{#1}{0}}{\fracreduce{}{#2}}
  {
	  \ifthenelse{\equal{#2}{1}}{\unitfrac[#1]{1}{64}}{}
	  \ifthenelse{\equal{#2}{2}}{\unitfrac[#1]{1}{32}}{}
	  \ifthenelse{\equal{#2}{3}}{\unitfrac[#1]{3}{64}}{}
	  \ifthenelse{\equal{#2}{4}}{\unitfrac[#1]{1}{16}}{}
	  \ifthenelse{\equal{#2}{5}}{\unitfrac[#1]{5}{64}}{}
	  \ifthenelse{\equal{#2}{6}}{\unitfrac[#1]{3}{32}}{}
	  \ifthenelse{\equal{#2}{7}}{\unitfrac[#1]{7}{64}}{}
	  \ifthenelse{\equal{#2}{8}}{\unitfrac[#1]{1}{8}}{}
	  \ifthenelse{\equal{#2}{9}}{\unitfrac[#1]{9}{64}}{}
	  \ifthenelse{\equal{#2}{10}}{\unitfrac[#1]{5}{32}}{}
	  \ifthenelse{\equal{#2}{11}}{\unitfrac[#1]{11}{64}}{}
	  \ifthenelse{\equal{#2}{12}}{\unitfrac[#1]{3}{16}}{}
	  \ifthenelse{\equal{#2}{13}}{\unitfrac[#1]{13}{64}}{}
	  \ifthenelse{\equal{#2}{14}}{\unitfrac[#1]{7}{32}}{}
	  \ifthenelse{\equal{#2}{15}}{\unitfrac[#1]{15}{64}}{}
	  \ifthenelse{\equal{#2}{16}}{\unitfrac[#1]{1}{4}}{}
	  \ifthenelse{\equal{#2}{17}}{\unitfrac[#1]{17}{64}}{}
	  \ifthenelse{\equal{#2}{18}}{\unitfrac[#1]{9}{32}}{}
	  \ifthenelse{\equal{#2}{19}}{\unitfrac[#1]{19}{64}}{}
	  \ifthenelse{\equal{#2}{20}}{\unitfrac[#1]{5}{16}}{}
	  \ifthenelse{\equal{#2}{21}}{\unitfrac[#1]{21}{64}}{}
	  \ifthenelse{\equal{#2}{22}}{\unitfrac[#1]{11}{32}}{}
	  \ifthenelse{\equal{#2}{23}}{\unitfrac[#1]{23}{64}}{}
	  \ifthenelse{\equal{#2}{24}}{\unitfrac[#1]{3}{8}}{}
	  \ifthenelse{\equal{#2}{25}}{\unitfrac[#1]{25}{64}}{}
	  \ifthenelse{\equal{#2}{26}}{\unitfrac[#1]{13}{32}}{}
	  \ifthenelse{\equal{#2}{27}}{\unitfrac[#1]{27}{64}}{}
	  \ifthenelse{\equal{#2}{28}}{\unitfrac[#1]{7}{16}}{}
	  \ifthenelse{\equal{#2}{29}}{\unitfrac[#1]{29}{64}}{}
	  \ifthenelse{\equal{#2}{30}}{\unitfrac[#1]{15}{32}}{}
	  \ifthenelse{\equal{#2}{31}}{\unitfrac[#1]{31}{64}}{}
	  \ifthenelse{\equal{#2}{32}}{\unitfrac[#1]{1}{2}}{}
	  \ifthenelse{\equal{#2}{33}}{\unitfrac[#1]{33}{64}}{}
	  \ifthenelse{\equal{#2}{34}}{\unitfrac[#1]{17}{32}}{}
	  \ifthenelse{\equal{#2}{35}}{\unitfrac[#1]{35}{64}}{}
	  \ifthenelse{\equal{#2}{36}}{\unitfrac[#1]{9}{16}}{}
	  \ifthenelse{\equal{#2}{37}}{\unitfrac[#1]{37}{64}}{}
	  \ifthenelse{\equal{#2}{38}}{\unitfrac[#1]{19}{32}}{}
	  \ifthenelse{\equal{#2}{39}}{\unitfrac[#1]{39}{64}}{}
	  \ifthenelse{\equal{#2}{40}}{\unitfrac[#1]{5}{8}}{}
	  \ifthenelse{\equal{#2}{41}}{\unitfrac[#1]{41}{64}}{}
	  \ifthenelse{\equal{#2}{42}}{\unitfrac[#1]{21}{32}}{}
	  \ifthenelse{\equal{#2}{43}}{\unitfrac[#1]{43}{64}}{}
	  \ifthenelse{\equal{#2}{44}}{\unitfrac[#1]{11}{16}}{}
	  \ifthenelse{\equal{#2}{45}}{\unitfrac[#1]{45}{64}}{}
	  \ifthenelse{\equal{#2}{46}}{\unitfrac[#1]{23}{32}}{}
	  \ifthenelse{\equal{#2}{47}}{\unitfrac[#1]{47}{64}}{}
	  \ifthenelse{\equal{#2}{48}}{\unitfrac[#1]{3}{4}}{}
	  \ifthenelse{\equal{#2}{49}}{\unitfrac[#1]{49}{64}}{}
	  \ifthenelse{\equal{#2}{50}}{\unitfrac[#1]{25}{32}}{}
	  \ifthenelse{\equal{#2}{51}}{\unitfrac[#1]{51}{64}}{}
	  \ifthenelse{\equal{#2}{52}}{\unitfrac[#1]{13}{16}}{}
	  \ifthenelse{\equal{#2}{53}}{\unitfrac[#1]{53}{64}}{}
	  \ifthenelse{\equal{#2}{54}}{\unitfrac[#1]{27}{32}}{}
	  \ifthenelse{\equal{#2}{55}}{\unitfrac[#1]{55}{64}}{}
	  \ifthenelse{\equal{#2}{56}}{\unitfrac[#1]{7}{8}}{}
	  \ifthenelse{\equal{#2}{57}}{\unitfrac[#1]{57}{64}}{}
	  \ifthenelse{\equal{#2}{58}}{\unitfrac[#1]{29}{32}}{}
	  \ifthenelse{\equal{#2}{59}}{\unitfrac[#1]{59}{64}}{}
	  \ifthenelse{\equal{#2}{60}}{\unitfrac[#1]{15}{16}}{}
	  \ifthenelse{\equal{#2}{61}}{\unitfrac[#1]{61}{64}}{}
	  \ifthenelse{\equal{#2}{62}}{\unitfrac[#1]{31}{32}}{}
	  \ifthenelse{\equal{#2}{63}}{\unitfrac[#1]{63}{64}}{}
  }
}
Here's my working examples:

This code works:

Code: Select all

\documentclass[letterpaper]{report}
\usepackage{units}
\begin{document} 
It seems that \unitfrac[5]{5}{8} look nice here!
\end{document}
This code has weird output:

Code: Select all

\documentclass[letterpaper]{report}
\usepackage{fracreduce}
\begin{document} 
Why does \fracreduce{5}{40} take up so much space???
\end{document}
From my point of view, both pieces of code should be functionally identical, but I obviously don't know what I'm doing. FYI, I'm compiling these documents using Tex Live on Ubuntu 10.04.

Thanks in advance for any help!
Last edited by batonac on Mon May 09, 2011 7:20 pm, edited 1 time in total.

Recommended reading 2024:

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

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

batonac
Posts: 12
Joined: Fri Feb 26, 2010 5:41 pm

Custom fraction convertor produces strange space.

Post by batonac »

OK, so per the instructions here, http://tex.stackexchange.com/questions/ ... en-package, I figured it out. By adding a comment symbol (%) after each line, the extra space vanished!

Here's my new functional code:

Code: Select all

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{fracreduce}[04/06/2011 Converts database input (64ths) into latex fractions]

\RequirePackage{ifthen}
\RequirePackage{units}

% #1 is the whole number, #2 is the decimal, both fed from database
\newcommand{\fracreduce}[2]%
{%
	\ifthenelse{\equal{#1}{0}}{\fracreduce{}{#2}}%
	{%
		\ifthenelse{\equal{#2}{1}}{\unitfrac[#1]{1}{64}}{}%
		\ifthenelse{\equal{#2}{2}}{\unitfrac[#1]{1}{32}}{}%
		\ifthenelse{\equal{#2}{3}}{\unitfrac[#1]{3}{64}}{}%
		\ifthenelse{\equal{#2}{4}}{\unitfrac[#1]{1}{16}}{}%
		\ifthenelse{\equal{#2}{5}}{\unitfrac[#1]{5}{64}}{}%
		\ifthenelse{\equal{#2}{6}}{\unitfrac[#1]{3}{32}}{}%
		\ifthenelse{\equal{#2}{7}}{\unitfrac[#1]{7}{64}}{}%
		\ifthenelse{\equal{#2}{8}}{\unitfrac[#1]{1}{8}}{}%
		\ifthenelse{\equal{#2}{9}}{\unitfrac[#1]{9}{64}}{}%
		\ifthenelse{\equal{#2}{10}}{\unitfrac[#1]{5}{32}}{}%
		\ifthenelse{\equal{#2}{11}}{\unitfrac[#1]{11}{64}}{}%
		\ifthenelse{\equal{#2}{12}}{\unitfrac[#1]{3}{16}}{}%
		\ifthenelse{\equal{#2}{13}}{\unitfrac[#1]{13}{64}}{}%
		\ifthenelse{\equal{#2}{14}}{\unitfrac[#1]{7}{32}}{}%
		\ifthenelse{\equal{#2}{15}}{\unitfrac[#1]{15}{64}}{}%
		\ifthenelse{\equal{#2}{16}}{\unitfrac[#1]{1}{4}}{}%
		\ifthenelse{\equal{#2}{17}}{\unitfrac[#1]{17}{64}}{}%
		\ifthenelse{\equal{#2}{18}}{\unitfrac[#1]{9}{32}}{}%
		\ifthenelse{\equal{#2}{19}}{\unitfrac[#1]{19}{64}}{}%
		\ifthenelse{\equal{#2}{20}}{\unitfrac[#1]{5}{16}}{}%
		\ifthenelse{\equal{#2}{21}}{\unitfrac[#1]{21}{64}}{}%
		\ifthenelse{\equal{#2}{22}}{\unitfrac[#1]{11}{32}}{}%
		\ifthenelse{\equal{#2}{23}}{\unitfrac[#1]{23}{64}}{}%
		\ifthenelse{\equal{#2}{24}}{\unitfrac[#1]{3}{8}}{}%
		\ifthenelse{\equal{#2}{25}}{\unitfrac[#1]{25}{64}}{}%
		\ifthenelse{\equal{#2}{26}}{\unitfrac[#1]{13}{32}}{}%
		\ifthenelse{\equal{#2}{27}}{\unitfrac[#1]{27}{64}}{}%
		\ifthenelse{\equal{#2}{28}}{\unitfrac[#1]{7}{16}}{}%
		\ifthenelse{\equal{#2}{29}}{\unitfrac[#1]{29}{64}}{}%
		\ifthenelse{\equal{#2}{30}}{\unitfrac[#1]{15}{32}}{}%
		\ifthenelse{\equal{#2}{31}}{\unitfrac[#1]{31}{64}}{}%
		\ifthenelse{\equal{#2}{32}}{\unitfrac[#1]{1}{2}}{}%
		\ifthenelse{\equal{#2}{33}}{\unitfrac[#1]{33}{64}}{}%
		\ifthenelse{\equal{#2}{34}}{\unitfrac[#1]{17}{32}}{}%
		\ifthenelse{\equal{#2}{35}}{\unitfrac[#1]{35}{64}}{}%
		\ifthenelse{\equal{#2}{36}}{\unitfrac[#1]{9}{16}}{}%
		\ifthenelse{\equal{#2}{37}}{\unitfrac[#1]{37}{64}}{}%
		\ifthenelse{\equal{#2}{38}}{\unitfrac[#1]{19}{32}}{}%
		\ifthenelse{\equal{#2}{39}}{\unitfrac[#1]{39}{64}}{}%
		\ifthenelse{\equal{#2}{40}}{\unitfrac[#1]{5}{8}}{}%
		\ifthenelse{\equal{#2}{41}}{\unitfrac[#1]{41}{64}}{}%
		\ifthenelse{\equal{#2}{42}}{\unitfrac[#1]{21}{32}}{}%
		\ifthenelse{\equal{#2}{43}}{\unitfrac[#1]{43}{64}}{}%
		\ifthenelse{\equal{#2}{44}}{\unitfrac[#1]{11}{16}}{}%
		\ifthenelse{\equal{#2}{45}}{\unitfrac[#1]{45}{64}}{}%
		\ifthenelse{\equal{#2}{46}}{\unitfrac[#1]{23}{32}}{}%
		\ifthenelse{\equal{#2}{47}}{\unitfrac[#1]{47}{64}}{}%
		\ifthenelse{\equal{#2}{48}}{\unitfrac[#1]{3}{4}}{}%
		\ifthenelse{\equal{#2}{49}}{\unitfrac[#1]{49}{64}}{}%
		\ifthenelse{\equal{#2}{50}}{\unitfrac[#1]{25}{32}}{}%
		\ifthenelse{\equal{#2}{51}}{\unitfrac[#1]{51}{64}}{}%
		\ifthenelse{\equal{#2}{52}}{\unitfrac[#1]{13}{16}}{}%
		\ifthenelse{\equal{#2}{53}}{\unitfrac[#1]{53}{64}}{}%
		\ifthenelse{\equal{#2}{54}}{\unitfrac[#1]{27}{32}}{}%
		\ifthenelse{\equal{#2}{55}}{\unitfrac[#1]{55}{64}}{}%
		\ifthenelse{\equal{#2}{56}}{\unitfrac[#1]{7}{8}}{}%
		\ifthenelse{\equal{#2}{57}}{\unitfrac[#1]{57}{64}}{}%
		\ifthenelse{\equal{#2}{58}}{\unitfrac[#1]{29}{32}}{}%
		\ifthenelse{\equal{#2}{59}}{\unitfrac[#1]{59}{64}}{}%
		\ifthenelse{\equal{#2}{60}}{\unitfrac[#1]{15}{16}}{}%
		\ifthenelse{\equal{#2}{61}}{\unitfrac[#1]{61}{64}}{}%
		\ifthenelse{\equal{#2}{62}}{\unitfrac[#1]{31}{32}}{}%
		\ifthenelse{\equal{#2}{63}}{\unitfrac[#1]{63}{64}}{}%
	}%
}%
Post Reply