Math & ScienceHow do I prevent latex from inserting unwanted dollar signs?

Information and discussion about LaTeX's math and science related features (e.g. formulas, graphs).
Post Reply
Singularity
Posts: 156
Joined: Sat Jan 22, 2011 9:55 pm

How do I prevent latex from inserting unwanted dollar signs?

Post by Singularity »

The quoted code is supposed to represent instructions for using a calculator (MWE at the bottom). However, the latex compiler insists on inserting "$" in various places. For example, it puts a dollar sign prior to the text "x^3", then the output is an x raised to the third power, which would be great if the calculator had an x^3 button.

But the calculator has a "x" button, a "^" button and a "3" button, which I want to be printed exactly as "x^3".

On the other hand, there is an "x^2" button, so I do want that text to be interpreted as math. You can see how I put the dollar signs around that statement.

How can I get the latex compiler to stop inserting unwanted $s?
You can enter the integral from line 2 into your calculator as\newline
\texttt{\textsc{[math] 9 2[$\pi$]1/6(x^3 [>] +3/x) [2nd] [$x^2$] 1/4(x [$x^2$] +x^-2 [>] ) [$x^2$],x,1/2,x}}

Code: Select all

\documentclass[fleqn]{article}
\usepackage{amsfonts,amsmath,amssymb,amsthm,nicefrac}
\everymath{\displaystyle}
\setlength{\parindent}{0pt}
\setlength{\parskip}{\baselineskip}
\begin{document}

Alternately, you can enter the integral from line 2 into your calculator as\newline
\newline
\texttt{\textsc{[math] 9 2[$\pi$]1/6(x^3 [>] +3/x) [2nd] [$x^2$] 1/4(x [$x^2$] +x^-2 [>] ) [$x^2$],x,1/2,x}}

\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.

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

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

How do I prevent latex from inserting unwanted dollar signs?

Post by Johannes_B »

Like the backslash, or the dollar sign, the circumflex (caret) is a symbol that has to be treated in a different way, if you want to print it.

Code: Select all

\documentclass[fleqn]{article}
\usepackage{amsfonts,amsmath,amssymb,amsthm,nicefrac}
\everymath{\displaystyle}
\setlength{\parindent}{0pt}
\setlength{\parskip}{\baselineskip}
\newcommand{\calcinput}[1]{\begingroup\ttfamily#1\endgroup}
\begin{document}

Alternately, you can enter the integral from line 2 into your
calculator as%\newline
%\newline%Oh, please don't do that

\calcinput{\textsc{[math] 9 2[$\pi$]1/6(x\textasciicircum3 [>]
	+3/x) [2nd] [x$^2$]%note the difference
	1/4(x [$x^2$] +x\^{}-2 [>] )
[$x^2$],x,1/2,x}}

\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.
Singularity
Posts: 156
Joined: Sat Jan 22, 2011 9:55 pm

Re: How do I prevent latex from inserting unwanted dollar si

Post by Singularity »

Thansk, Johannes. What exactly was the part that told Latex not to insert dollar signs?

I notice you treated the circumflex differently in two different spots. Once you explicitly said "\circumflex" and once you just escaped it with a slash. What's the difference? Were you just showing me two different ways to accomplish the same thing?

I had tried escaping it, but it didn't work. I think I did it wrong.

Thanks.
User avatar
Johannes_B
Site Moderator
Posts: 4182
Joined: Thu Nov 01, 2012 4:08 pm

How do I prevent latex from inserting unwanted dollar signs?

Post by Johannes_B »

What told LaTeX not to get into math mode? Both are text commands.

Both ways are exactly the same

Code: Select all

\DeclareTextCommandDefault{\textasciicircum}{\^{}}
As you can see, it really escaping the char, but it is a placing the diacritic over an non-existing letter (the empty argument). I bet you forgot that pair of empty braces and suddenly the minus sign got a caret. At least this is what happended to me when i tried the first time ;-)
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
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

How do I prevent latex from inserting unwanted dollar signs?

Post by cgnieder »

Singularity wrote:What exactly was the part that told Latex not to insert dollar signs?
This minimal example

Code: Select all

\documentclass{article}
\begin{document}
^
\end{document}
gives in the log

Code: Select all

! Missing $ inserted.
<inserted text> 
                $
l.3 ^
     
I've inserted a begin-math/end-math symbol since I think
you left one out. Proceed, with fingers crossed.
In standard settings the character ^ has category code 7 (math superscript) which makes usage of ^ only allowed in math mode. If (La)TeX finds it in another mode it switches to math mode trying to fix the supposed error in the code.

Regards
site moderator & package author
Post Reply