LaTeX forum ⇒ Math & ScienceHow do I prevent latex from inserting unwanted dollar signs? Topic is solved

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

How do I prevent latex from inserting unwanted dollar signs?

Postby Singularity » Sun Jul 12, 2015 1:43 am

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}}


  1. \documentclass[fleqn]{article}
  2. \usepackage{amsfonts,amsmath,amssymb,amsthm,nicefrac}
  3. \everymath{\displaystyle}
  4. \setlength{\parindent}{0pt}
  5. \setlength{\parskip}{\baselineskip}
  6. \begin{document}
  7.  
  8. Alternately, you can enter the integral from line 2 into your calculator as\newline
  9. \newline
  10. \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}}
  11.  
  12. \end{document}

Tags:

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

Postby Johannes_B » Sun Jul 12, 2015 10:02 am

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.

  1. \documentclass[fleqn]{article}
  2. \usepackage{amsfonts,amsmath,amssymb,amsthm,nicefrac}
  3. \everymath{\displaystyle}
  4. \setlength{\parindent}{0pt}
  5. \setlength{\parskip}{\baselineskip}
  6. \newcommand{\calcinput}[1]{\begingroup\ttfamily#1\endgroup}
  7. \begin{document}
  8.  
  9. Alternately, you can enter the integral from line 2 into your
  10. calculator as%\newline
  11. %\newline%Oh, please don't do that
  12.  
  13. \calcinput{\textsc{[math] 9 2[$\pi$]1/6(x\textasciicircum3 [>]
  14. +3/x) [2nd] [x$^2$]%note the difference
  15. 1/4(x [$x^2$] +x\^{}-2 [>] )
  16. [$x^2$],x,1/2,x}}
  17.  
  18. \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: 152
Joined: Sat Jan 22, 2011 9:55 pm

Postby Singularity » Sun Jul 12, 2015 2:29 pm

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: 3790
Joined: Thu Nov 01, 2012 4:08 pm

Postby Johannes_B » Sun Jul 12, 2015 3:25 pm

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

Both ways are exactly the same
  1. \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: 1988
Joined: Sat Apr 16, 2011 7:27 pm

Postby cgnieder » Sun Jul 12, 2015 3:31 pm

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


This minimal example
  1. \documentclass{article}
  2. \begin{document}
  3. ^
  4. \end{document}

gives in the log
  1. ! Missing $ inserted.
  2. <inserted text>
  3. $
  4. l.3 ^
  5.  
  6. I've inserted a begin-math/end-math symbol since I think
  7. 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
Clemens
------------------------------
chemmacros · chemformula · leadsheets · xsim


Return to “Math & Science”

Who is online

Users browsing this forum: Baidu [Spider] and 3 guests