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

  • LaTeX
  • Thread starter evinda
  • Start date
  • Tags
    Change
In summary, the LaTeX symbol for the max function is missing, and the code may not work as intended because of it. There are several different LaTeX environments that can be used to write code, and the best option may depend on the specific needs of the project. Additionally, the verbatim environment lets you type in any special symbols you want, and the listings package can be used for actual source code.
  • #1
evinda
Gold Member
MHB
3,836
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: 89
Physics news on Phys.org
  • #2
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).
 
  • #3
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)
 
  • #4
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).
 
  • #5
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)
 

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

What are the main features of Latex?

Latex is a typesetting language that is used for creating high-quality documents, including scientific papers, reports, and presentations. It offers a wide range of features such as automatic formatting, mathematical equations, tables, figures, and cross-referencing.

Can I change the font style and size in Latex?

Yes, Latex allows you to change the font style and size using various commands. You can choose from a variety of fonts, including serif, sans-serif, and monospace. The font size can be adjusted using the commands such as \tiny, \small, \large, and \Huge.

How do I insert images and tables in Latex?

To insert images in Latex, you can use the \includegraphics command and specify the file path of the image. For creating tables, you can use the \begin{tabular} and \end{tabular} commands to define the structure and content of the table.

Can I customize the layout of my document in Latex?

Yes, Latex allows you to customize the layout of your document using various commands and packages. You can adjust the margins, headers and footers, and the overall structure of the document to suit your needs.

Is it possible to collaborate with others on a Latex document?

Yes, Latex documents can be easily shared and collaborated on using version control systems such as Git or online platforms like Overleaf. These tools allow multiple users to work on the same document simultaneously and track changes made by each user.

Similar threads

Back
Top