Math & Sciencecustom stackrel command with proper line/equation placement

Information and discussion about LaTeX's math and science related features (e.g. formulas, graphs).
Post Reply
evencoil
Posts: 9
Joined: Sun Dec 02, 2007 11:28 pm

custom stackrel command with proper line/equation placement

Post by evencoil »

I would like to make a custom command that does
\stackrel{d}{\rightarrow}
when inside an equation, but does
\rightarrow_{d}
when (in mathmode) in a paragraph. (In order to avoid spreading lines in paragraphs.)

I know how to make very simple custom commands, but this is a step beyond my knowledge. Can anyone give me a hint of how I would go about doing this? Thanks.

Recommended reading 2024:

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

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

User avatar
localghost
Site Moderator
Posts: 9202
Joined: Fri Feb 02, 2007 12:06 pm

custom stackrel command with proper line/equation placement

Post by localghost »

There is a command which makes LaTeX not to regard the height of the box containing your operator. Additionally you should decrease the size of the upper symbol not to make it look too bad in the line.

Code: Select all

$x\smash{\stackrel{\scriptscriptstyle d}{\rightarrow}}y$

Best regards
Thorsten¹
evencoil
Posts: 9
Joined: Sun Dec 02, 2007 11:28 pm

Re: custom stackrel command with proper line/equation placement

Post by evencoil »

thank you, that works well for stopping the line-spreading and I think I might use that, although its not quite what I was looking for...

My idea was more along the line of how \lim works. If I use \lim_{n \rightarrow \infty} in a paragraph then the "n \rightarrow \infty" part ends up on the lower right of "lim" rather than below "lim" as it would if I used the command inside an equation environment. Do you know what the logic is that creates that and how I could apply it to this problem?
phi
Posts: 577
Joined: Tue Oct 21, 2008 8:10 pm

custom stackrel command with proper line/equation placement

Post by phi »

Math atoms of class 1 take limits when inserted in display style:

Code: Select all

\documentclass{article}

\usepackage{amsmath}

\newcommand*\test{\mathop\rightarrow^x}


\begin{document}

$a \test b$

\begin{align*}
	a \test b
\end{align*}

\end{document}
evencoil
Posts: 9
Joined: Sun Dec 02, 2007 11:28 pm

custom stackrel command with proper line/equation placement

Post by evencoil »

so now i'm being quite picky, but this is partly as an interest in learning TeX better. In that one the expression appearing in the paragraph is superscripted, whereas I would prefer it to be subscripted. This seems to require some additional logic. I was trying something like this using the ifthen package:

Code: Select all

\documentclass{article}

\usepackage{amsmath, ifthen}

\newcommand*\test{\ifthenelse{\boolean{???}}{\mathop\rightarrow^x}{\mathop\rightarrow_{x}}}

\begin{document}

$a \test b$

\begin{align*}
   a \test b
\end{align*}

\end{document}
except I can't find a TeX boolean for paragraph versus equation (there's one for mmode but that treats paragraphs and equations the same).
phi
Posts: 577
Joined: Tue Oct 21, 2008 8:10 pm

custom stackrel command with proper line/equation placement

Post by phi »

Then you need to distinguish between the for math styles using \mathchoice:

Code: Select all

\documentclass{article}

\usepackage{amsmath}

\newcommand*\testA{\mathop{\mkern0mu\rightarrow}^x}
\newcommand*\testB{\mathchoice{\stackrel x\rightarrow}{\rightarrow_x}{\rightarrow_x}{\rightarrow_x}}


\begin{document}

$a \testA b \testB c$

\begin{align*}
	&a \testA b \testB c \\
	&\frac{\frac{\frac\testB\testB}\testB}\testB
\end{align*}

\end{document}
evencoil
Posts: 9
Joined: Sun Dec 02, 2007 11:28 pm

custom stackrel command with proper line/equation placement

Post by evencoil »

that is excellent, thank you. This \mathchoice command is going to help me out in a lot of other regards as well. Many thanks!
Post Reply