Can I Change Latex Syntax to Avoid 'Missing $ inserted' Error?

  • Context: LaTeX 
  • Thread starter Thread starter evinda
  • Start date Start date
  • Tags Tags
    Change
Click For Summary
SUMMARY

The discussion addresses the 'Missing $ inserted' error in LaTeX when using underscores in variable names like MAX_POS. It emphasizes that writing algorithms in math mode is inappropriate and recommends using the verbatim environment for special symbols without formatting. For pseudocode, the algorithm2e package is suggested, while the listings package is ideal for actual source code with syntax highlighting. Users are reminded that numerous LaTeX packages exist beyond amsmath, and installation of additional packages may be necessary.

PREREQUISITES
  • Familiarity with LaTeX syntax and environments
  • Understanding of the verbatim environment in LaTeX
  • Knowledge of the algorithm2e package for pseudocode formatting
  • Experience with the listings package for source code representation
NEXT STEPS
  • Explore the verbatim environment in LaTeX for handling special characters
  • Learn how to implement the algorithm2e package for writing algorithms
  • Investigate the listings package for enhanced source code formatting
  • Research additional LaTeX packages for specific formatting needs
USEFUL FOR

LaTeX users, technical writers, and anyone involved in typesetting algorithms or source code in documents.

evinda
Gold Member
MHB
Messages
3,741
Reaction score
0
Hello!
I am writing something in Latex and I am facing some difficulties.

If I write [m] MAX_POS[/m] I get the message [m] Missing $ inserted[/m] because of this _ . So can't I use this symbol? (Worried)

Also it looks like this:

View attachment 3782How could I change it so that the while loop that is at lines 3,4 is only at one line? (Thinking)
 

Attachments

  • TPhoto_00096.jpg
    TPhoto_00096.jpg
    19.8 KB · Views: 126
Physics news on Phys.org
Writing algorithms in math mode is a mistake. Fortunately, there are plenty of LaTeX environments designed to handle pseudocode or even actual code. The simplest option is using the verbatim environment, which offers no formatting but let's you type any special symbols you want, and displays them in a typewriter font. Example:

Code:
\begin{verbatim}
int i = 0, j = 0;
int x = 2;

while (i + j < x) {
    i = i + (x mod j)
    x++
}
\end{verbatim}

For writing algorithms in particular, you can use algorithm2e, which is a bit complicated but let's you write up pseudocode and have it take care of the formatting: LaTeX/Algorithms - Wikibooks, open books for an open world

Finally, for actual source code the listings package is probably the best option, it works much like the verbatim example above, except you use listings and optionally configure it if you want a special look/need special syntax highlighting: LaTeX/Source Code Listings - Wikibooks, open books for an open world

Remember that there is more than the amsmath package in LaTeX! If you need to write something in LaTeX, chances are someone has written a package for it. (note you may need to install extra packages if they aren't provided by default, but on a properly configured LaTeX environment this is done automatically and even if it isn't it's not hard to install them manually).
 
Bacterius said:
Writing algorithms in math mode is a mistake. Fortunately, there are plenty of LaTeX environments designed to handle pseudocode or even actual code. The simplest option is using the verbatim environment, which offers no formatting but let's you type any special symbols you want, and displays them in a typewriter font. Example:

Code:
\begin{verbatim}
int i = 0, j = 0;
int x = 2;

while (i + j < x) {
    i = i + (x mod j)
    x++
}
\end{verbatim}

For writing algorithms in particular, you can use algorithm2e, which is a bit complicated but let's you write up pseudocode and have it take care of the formatting: LaTeX/Algorithms - Wikibooks, open books for an open world

Finally, for actual source code the listings package is probably the best option, it works much like the verbatim example above, except you use listings and optionally configure it if you want a special look/need special syntax highlighting: LaTeX/Source Code Listings - Wikibooks, open books for an open world

Remember that there is more than the amsmath package in LaTeX! If you need to write something in LaTeX, chances are someone has written a package for it. (note you may need to install extra packages if they aren't provided by default, but on a properly configured LaTeX environment this is done automatically and even if it isn't it's not hard to install them manually).
Nice! Thanks a lot! (Smile)

Now I wrote a long comment but it is cut from a point.
What could I do to change the line so that the whole comment is appeared? (Thinking)
 
evinda said:
Nice! Thanks a lot! (Smile)

Now I wrote a long comment but it is cut from a point.
What could I do to change the line so that the whole comment is appeared? (Thinking)

Insert a line break (press enter)? The verbatim environment doesn't do line wrapping for you, it literally just takes what you write and pastes it directly into the output document. Also maybe consider reducing the font size a notch for your verbatim block, enough for it to be still readable but save some space (in my opinion it is too large by default on most document styles).
 
Bacterius said:
Insert a line break (press enter)? The verbatim environment doesn't do line wrapping for you, it literally just takes what you write and pastes it directly into the output document. Also maybe consider reducing the font size a notch for your verbatim block, enough for it to be still readable but save some space (in my opinion it is too large by default on most document styles).

Nice... Thanks a lot! (Nod)
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 36 ·
2
Replies
36
Views
2K
  • · Replies 2 ·
Replies
2
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
Replies
3
Views
4K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 2 ·
Replies
2
Views
6K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 4 ·
Replies
4
Views
4K