Text FormattingProblem with spaces after \newcommand-command

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
LarsWa
Posts: 5
Joined: Fri Nov 07, 2014 12:36 am

Problem with spaces after \newcommand-command

Post by LarsWa »

I want to have a short command for “degree Celsius” and have tried different ways, but they either give me spaces between oC and punctuation marks, or no spaces between oC and the following word. See my mini-example below. How can I make this work (or: where can I find such a command?)

Code: Select all

\documentstyle{book}
\newcommand{\oCA}{$\:^\circ$C}
\newcommand{\oCB}{$\:^\circ$C }
\begin{document}
An example: if a piece of ice is left in a room at 20\oCA, it will melt. In thermodynamic terminology, the equilibrium state of water at 20\oCA is the liquid state, so ice tends to be transformed to liquid water; melting is in this case a spontaneous process.

An example: if a piece of ice is left in a room at 20\oCB, it will melt. In thermodynamic terminology, the equilibrium state of water at 20\oCB is the liquid state, so ice tends to be transformed to liquid water; melting is in this case a spontaneous process.
\end{document}

Recommended reading 2024:

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

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

Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

Problem with spaces after \newcommand-command

Post by Johannes_B »

Oh WOW, are you really using documentstyle? This is deprecated for 20 years.

A modern package can do the job reliably. For example, Joseph Wright wrote siunitx a very powerful package denoted to typesetting siunits in almost any possible way. You will need an up to date distribution, but you can have a peak at the output by clicking at »Open in writelatex« just above the code.

Code: Select all

\documentclass{book}
\usepackage{siunitx}
\begin{document}
An example: if a piece of ice is left in a room at \SI{20}{\celsius}, it will melt. In thermodynamic terminology, the equilibrium state of water at \SI{20}{\celsius} is the liquid state, so ice tends to be transformed to liquid water; melting is in this case a spontaneous process.
\end{document}
The smart way: Calm down and take a deep breath, read posts and provided links attentively, try to understand and ask if necessary.
User avatar
Stefan Kottwitz
Site Admin
Posts: 10340
Joined: Mon Mar 10, 2008 9:44 pm

Problem with spaces after \newcommand-command

Post by Stefan Kottwitz »

Hi Lars,

also I would use \documentclass (LaTeX 2e) instead of \documentstyle (LaTeX 2.09), otherwise even \usepackage would not work.

Johannes advice regarding siunitx is very good. The original problem was the spacing after the new command, for this you could use the xspace package. It takes care of spacing after the macro, such as having a space before the next word but not before punctuation (in common situations). siunitx can use xspace for defining units.

Here's how it can be done:

Code: Select all

\documentclass{book}
\usepackage{siunitx}
\usepackage{xspace}
\newcommand{\oCA}{$\:^\circ$C\xspace}
\newcommand{\oCB}[1]{\SI{#1}{\celsius}}
\begin{document}
An example: if a piece of ice is left in a room at 20\oCA, it will melt. In thermodynamic terminology, the equilibrium state of water at 20\oCA is the liquid state, so ice tends to be transformed to liquid water; melting is in this case a spontaneous process.

An example: if a piece of ice is left in a room at \oCB{20}, it will melt. In thermodynamic terminology, the equilibrium state of water at \oCB{20} is the liquid state, so ice tends to be transformed to liquid water; melting is in this case a spontaneous process.
\end{document}
Stefan
LaTeX.org admin
LarsWa
Posts: 5
Joined: Fri Nov 07, 2014 12:36 am

Re: Problem with spaces after \newcommand-command

Post by LarsWa »

Thank you. I do use documentclass... but before posting the problem I went back to Lamports book to get ideas of what the problem could be...and then I reverted to old style LaTeX documentstyle by mistake.
Post Reply