0

I am new to LaTeX. When I tried to write an algorithm all spaces between words get removed after \gets. See the image.

\begin{algorithm}
\caption{Filtering lexicon file}
\label{algo1}
\begin{algorithmic}[1]
\State $Lex$ \gets read the whole lexicon file.$
\end{algorithmic}
\end{algorithm}

enter image description here

How to keep the spaces between words "read the whole lexicon file". Thank you.

  • 1
    You get errors from that input, because \gets is a math symbol. Adding a trailing $ is not the solution. Maybe $\mathit{Lex}\gets$ read the whole lexicon file. – egreg Mar 23 at 16:13
  • @egreg post it as a answer. $\mathit{Lex}\gets$ read the whole lexicon file. is the correct answer. I will make it my answer. – Nahid Hossain Shihab Mar 23 at 16:20
  • 1
    @NahidHossain I believe you should extend the example, so that it's possible to give advice also for other parts of the algorithm. – egreg Mar 23 at 16:21
1

There are several ways to get a correction and it may depend on other usage

I am not saying these are more correct but the simplest is to remove the $ as per line 1 but that will change the font upright so see some alternatives with italics enter image description here

\documentclass[10pt]{report}
\usepackage{algpseudocode}% http://ctan.org/pkg/algorithmicx
\usepackage{algorithm}
\usepackage[utf8x]{inputenc}
\usepackage{amsmath,graphicx,subfigure}
\begin{document}
\begin{algorithm}
\caption{Filtering lexicon file}
\label{algo1}
\begin{algorithmic}[1]
\State $Lex \gets $ read the whole lexicon file.
\State $Lex \gets~read~the~whole~lexicon~file.$
\State $Lex \gets \textit{read the whole lexicon file.}$
\State $Lex \gets $ \textit {read the whole lexicon file.}
\State $Lex \gets $ {\itshape read the whole lexicon file.} % as recommended by Barbara Beeton This switch should be bound
\State $Lex \gets $ read the whole lexicon file. % note none of the above have affected this line

here is another line
\end{algorithmic}
and another
\end{algorithm}

and another \end{document}

  • While it may work okay here, i.e., get turned off at the end of the line (I'm not sure, and can't test), an "ungrouped" \itshape will persist in other situations, and should be used with caution. – barbara beeton Mar 23 at 17:05
  • @barbarabeeton I am no TeXnician so accept that all my suggestions could be wrong its just that in my editor(s) they worked as per their fonts, I accept they may likely behave badly in other engines/locales – KJO Mar 23 at 17:07
  • If I were trying to test the persistence, I would repeat at the end the first instance of the test line (the one with no style change after the second $) to make sure it doesn't become italic. (Sorry for the noise; I would have tested if I could, but have no TeX installation available.) – barbara beeton Mar 23 at 17:19
  • @barbarabeeton in my laymans terms as far as I can tell \itshape persists with or without braces until it is "broken" by the \end{algorithmic} thus could affect following lines within that environment – KJO Mar 23 at 17:25
  • Given that you've determined that it persists, it would be best to add braces to that step -- {\itshape ... }. Font "switches" like \itshape are intended to be used in localized situations. (An environment like algorithmic is almost always defined as a local group.) – barbara beeton Mar 23 at 17:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.