Math & Sciencesiunitx: Document-wide parameters

Information and discussion about LaTeX's math and science related features (e.g. formulas, graphs).
itsmereally
Posts: 32
Joined: Sun Jul 25, 2010 11:15 pm

siunitx: Document-wide parameters

Post by itsmereally »

Hello

I've just started using siunitx, and I was wondering if it was possible to set parameters before the \begin{document} command so that they would automatically be applied any time I use the \si{...} command.

What I would like to do is always have my units italicised and coloured green.

Is this possible? I'd rather not manually add these parameters each time.

Thanks. :)

Recommended reading 2024:

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

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

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

josephwright
Site Moderator
Posts: 814
Joined: Tue Jul 01, 2008 2:19 pm

siunitx: Document-wide parameters

Post by josephwright »

In the preamble

Code: Select all

\sisetup{unit-color = green, unit-math-rm = \mathit}
should do it. This assumes you are using siunitx v2: for v1 the option names are different but the concept is the same.

If you only want to affect the \si macro, but not other siunitx macros, then you will need to make your own copy. Something like:

Code: Select all

\let\Origsi\si
\RenewDocumentCommand\si{O{}m}{%
  \Origsi[unit-color = green, unit-math-rm = \mathit,#1]{#2}%
}
Joseph Wright
itsmereally
Posts: 32
Joined: Sun Jul 25, 2010 11:15 pm

Re: siunitx: Document-wide parameters

Post by itsmereally »

Thanks for the reply.

I'm sorry I didn't get back to you sooner. I've been a bit distracted.

I get an "xkeyval" error using the text from the first code box. I don't know which version I'm using, so what would the commands be for v1 of siunitx?
josephwright
Site Moderator
Posts: 814
Joined: Tue Jul 01, 2008 2:19 pm

siunitx: Document-wide parameters

Post by josephwright »

I think you want

Code: Select all

\let\Origsi\si
\renewcommand*\si[2][]{%
  \Origsi[unitcolor = green, unitmathrm = mathit,#1]{#2}%
}
Joseph Wright
itsmereally
Posts: 32
Joined: Sun Jul 25, 2010 11:15 pm

Re: siunitx: Document-wide parameters

Post by itsmereally »

I'm getting a babel error, saying I haven't loaded the option english.

How do I go about getting v2 of siunitx? The computer I use for my documents doesn't have internet access, so I'd need to sort it out manually.

I didn't realise you were the creator of the package. Thank you very much for all your effort. It's making my documents much better. :)
josephwright
Site Moderator
Posts: 814
Joined: Tue Jul 01, 2008 2:19 pm

Re: siunitx: Document-wide parameters

Post by josephwright »

The babel error is nothing to do with siunitx, I think. A full example will be needed to get to the bottom of this.

As I said in my previous answer, you can achieve the effect you want with version 1 of siunitx. However, if you do want version 2 then download siunitx.tds.zip from CTAN and do a 'local installation'. See http://www.tex.ac.uk/cgi-bin/texfaq2htm ... st-tds-zip and links therein for more information.
Joseph Wright
itsmereally
Posts: 32
Joined: Sun Jul 25, 2010 11:15 pm

siunitx: Document-wide parameters

Post by itsmereally »

Since I tried the \sisetup{...}, I get a babel error with everything.

Code: Select all

\documentclass{article}
\begin{document}
Small is beautiful.
\end{document}
Even this, which I used to make sure TexWorks had installed properly, now gives me "! Package babel Error: You haven't loaded the option english yet."

I'm really confused. I can't use TexWorks on this computer now, because of this error. It just won't compile anything.
User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

siunitx: Document-wide parameters

Post by localghost »

itsmereally wrote:[...] Even this, which I used to make sure TexWorks had installed properly, now gives me "! Package babel Error: You haven't loaded the option english yet."

I'm really confused. I can't use TexWorks on this computer now, because of this error. It just won't compile anything.
Sounds very strange. Usually this error occurs when switching from one language option to another but disappears with the next compiler run. It can be caused by entries in several auxiliary files (*.aux, *.toc, ...) where some fragments still contain commands for the former language.

Try to compile on the command line and see what happens. I can't imagine that the installation of the newer package version has caused this trouble.


Thorsten
itsmereally
Posts: 32
Joined: Sun Jul 25, 2010 11:15 pm

Re: siunitx: Document-wide parameters

Post by itsmereally »

Well, it seems to be working now. I haven't done anything different, other than try it on a different day. I didn't install a new package. I got the babel error after trying the \sisetup{...} command. siunitx was already installed. :)

I'm going to have a play around with that command and see what I can get working.

Thank you very much Joseph and localghost. :)
itsmereally
Posts: 32
Joined: Sun Jul 25, 2010 11:15 pm

siunitx: Document-wide parameters

Post by itsmereally »

Thought I'd give an update.

I've managed to get things working now. The babel error was caused by the very first line of the documents I was experimenting with. I had \documentclass{article}, but changing to \documentclass[a4paper]{article} solved the babel error.

I've also managed to get siunitx v1 working PERFECTLY. I love it. I'm so glad local_ghost told me about. :)

Thanks again for the help. I can't seem to edit my initial post though, so I can't put the green "check" icon on the topic.
Post Reply