2

I got a problem with formatting text in math mode. I made a commmand that moves the unit in the end of the equations all the way to the right. It looks like that

    \newcommand{\unit}[1]{\hfill{\text{#1}}}    

And this is how I intended to use it (just some random equation):

\begin{equation}
p(P)=S_{\text{i}}+10\cdot \log_{10}\left(\frac{P}{1\:\text{W}}\right)\,\unit[{dB}_{SPL}] 
\end{equation}

And this is how it looks:

enter image description here

And now to the problem. As you can se the unit is italic but I would like to be normal (roman). I can do this if I type it manually every time I add a new unit but I would like it to automatic.

I have tried to use:

\mbox
\mathrm
\normalfont
\text
\textrm

Here is some more info you might need to help me:

\documentclass[a4paper,11pt,fleqn,dvipsnames,twoside,openright]{memoir} 
\usepackage[utf8]{inputenc}
\usepackage[danish]{babel}
\usepackage[T1]{fontenc}
\usepackage{ragged2e,anyfontsize}
\usepackage{fixltx2e}

\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{stmaryrd}
\usepackage{amsthm}
\usepackage{textcomp}
\usepackage{siunitx}
\sisetup{output-decimal-marker = {,}}
\newcommand{\e}{\text{e}}
%THE newcommand which is causing problems!%
\newcommand{\enhed}[1]{\hfill{\text{#1}}}

\hyphenation{}
5
  • 2
    Hi and welcome, are you aware of package siunitx? You are loading it, why not use it here? Does something like \si{db_{SPL}} not work?
    – Johannes_B
    Oct 22, 2014 at 17:45
  • Thanks Jhannes - And yes it dose something with si units but I haven't used it and I dont know if you can make it automatically.Edit: no \si{} didnt work got a Undefined control sequence error.
    – Vinkel
    Oct 22, 2014 at 17:49
  • Welcome to TeX.SX! Please make your code into a complete, small document which is compilable (if possible). That makes it a lot easier for people to help: they can just copy-paste your code to reproduce the problem.
    – cfr
    Oct 22, 2014 at 17:56
  • in \unit[{dB}_{SPL}] the argument of \unit is just [ the other characters are just set in the surrounding math expression Oct 22, 2014 at 17:56
  • #3 I will try that next time :) #4 Thanks for the help its working now :)
    – Vinkel
    Oct 22, 2014 at 18:06

2 Answers 2

4

You're using the definition of \unit incorrectly.

\newcommand{\unit}[1]{..}

specifies that \unit will take a single mandatory argument. If you don't provide an explicit brace (in the form of \unit{..}), then the first token will be taken as the argument:

\unit[{dB}_{SPL}]

So in the above, [ is taken as the argument and therefore the above is replaced with

\hfill\text{[}{dB}_{SPL}]

which sets as expected. You should use

\unit{dB_{SPL}}

with the definition of \unit

\newcommand{\unit}[1]{[\mathrm{#1}]}

Note though that \hfill doesn't make any sense in math mode and therefore the \unit will be set just to the right of where it's placed. I find that okay...

enter image description here

\documentclass{article}
\usepackage{amsmath}
\newcommand{\unit}[1]{\hfill\text{}[\mathrm{#1}]}

\begin{document}

\begin{equation}
  p(P) = S_{\text{i}}+10\cdot \log_{10}\left(\frac{P}{1\:\text{W}}\right)\,\unit{dB_{SPL}}
\end{equation}

\end{document}
0
2

Although the answer of Werner totally and correctly adresses the question of how to technically solve the problem, there is a big drawback: the use of units and brackets wished by the questioner is simply wrong.

Please note that [X] reads as the unit of X. So something like this would be correct: $[I] = \mathrm{A}$ or better $[I] = \si{\ampere}$. Read: "The unit of the current is Ampere".

So the physical quantity goes into the brackets, not the unit. The physical quantity itself is a product of the value and its unit .

So there would be two correct ways of showing your equation, please note also the use of siunitx for the "1 W" :

enter image description here

\documentclass{article}
\usepackage{amsmath}
\usepackage{siunitx}

\begin{document}

\begin{equation}
  p(P) = \left( S_{\text{i}}+10\cdot \log_{10}\!\left(\frac{P}{\SI{1}{\watt}}\right) \right) \si{\deci\bel}
\end{equation}

\begin{equation}
  p(P)/\si{\deci\bel} = S_{\text{i}}+10\cdot \log_{10}\!\left(\frac{P}{\SI{1}{\watt}}\right)
\end{equation}

\end{document}

Also the spacing in front of the \left( parentheses is not optimal, i added a negative math space \!

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .