- IN Packages
Beautiful fractions and units with the unit package
- Mar 8, 2011 |
- 9 Comments
When dealing with mathematics, especially with fractions, you might not be satisfied with the way \frac (and \dfrac) look. Me neither! Especially in inline fractions. Similar, when dealing with physical units, you might want them to be displayed nicer and with less effort. Here’s how to do those two things with the units package. Note that the nicefrac package does the same thing for just fractions.
Usage
First of all, the package is defined in your preamble as
\usepackage[]{units} |
Between the square brackets you can define one or more options:
- tight (Default) or loose, for the spacing of units
- nice (Default) or ugly, for nice or ugly (!) fractions
Fractions
The nice (Default) and ugly options are regarding the the fractions in your document. The things explained in this section can also be when using the nicefrac package.
Very briefly, here’s how to use the nicer fractions. Implement them in your LaTeX document with the \nicefrac command:
\nicefrac[]{1}{2} |
Between the square brackets you can put font commands, like \texttt, \mathcal or whatever you like (the \nicefrac command can deal even with quite strange font changing commands!)
Units
A unit can be called as follows:
\unit[val]{dim} |
where val represents the value and dim the dimension. For instance, if you want to print ’5 meters’, you put:
\unit[5]{m} |
When leaving the square brackets out, only the dimension is printed.
Now for the fractions of units (like meters per second):
\unitfrac[val]{num}{den} |
The val option is the same as above. The num and den commands are pretty straight-forward, being the numerator and the denominator respectively. As an example, if you want to print ’5 meters per second’, you put:
\unitfrac[5]{m}{s} |
The SIunits package
Habi posted a comment on this post where he pointed to the SIunits package. This package let’s you deal with SI units in a very intuitive way. I suggest you read (part of) the package documentation before you use it, but here’s an example how to combine SIunits with units:
\unitfrac[5]{\metre}{\second} |
Now this doesn’t seem advantageous at all, but when you’re units are getting more complicated the SIunits package will make sure you make no mistakes!
Source
The idea of this post came after reading this post.
[...] Unit formatting tips are given at howToTeX.com: Beautiful fractions and units with the unit package [...]
When I use \unitfrac, it works great, but when I use \unit, the square brackets print as well. For example:
\unit[1.5]{GHz}produces something like: [ 1.5]GHz in the pdf.
Any idea why this is happening?
Hi sr,
I have no idea why that’s happening. However, as can be read in the previous comments, the units package is quite out-dated.. You should try the siunitx package, which is way better than the units package in my opinion.
I actually mentioned the siunitX package :)
Oops! Must have been asleep when I read your comment.. Thanks all!
Maybe you should add information about the siunitx package, seeing it is the successor of SIunits?
Hi tentotwo, thanks for the heads up! I think I’ll write a separate post on the SIunitx package some day :-)
For typesetting units I suggest you take a look at the siunitx package, it’s extremely useful for this. Doesn’t do nice fractions though :)
Hi Habi, the
SIunitspackage is indeed great and it’s actually worthy to mention it in this post. I’ll add that, thank you!