3

I'm using Lyx software to typeset an algorithm. I'm trying to use the \quad command to indent in a loop I'm writing, yet the \quad command is not being recognized when I compile the page. I'm assuming I need to load a particular package to use the \quad command. Does anyone know what package I should load? Or if it's not a package loading problem, what do I need to do so that I can use \quad.

  • 1
    Please refer to this. tex.stackexchange.com/questions/41476/… – Harry Mar 31 '14 at 1:03
  • 1
    Welcome to TeX.SX! A tip: You can use backticks ` to mark your inline code as I did in my edit. – Adam Liter Mar 31 '14 at 1:15
  • Make sure to insert it as actual TeX code; plain old text won't be recognized. I believe the shortcut is Ctrl+R. – Sean Allred Mar 31 '14 at 1:46
  • 1
    Beware of using \quad for indenting, because \hspace is removed at the beginning of a line (except the first in a paragraph). – egreg Mar 31 '14 at 7:39
  • 1
    @SeanAllred Ctrl + L, or Insert --> TeX Code. – Torbjørn T. Mar 31 '14 at 9:00
5

You don't need any additional packages to use \quad. If by 'not recognized' you mean that \quad doesn't have an effect, it may be because you've added it after a line break (Ctrl + Enter), where it won't work, as egreg mentioned in a comment. Instead, use \hspace*{1em} in an ERT, or do Insert --> Formatting --> Horizontal space, using a custom length and with Protect checked:

enter image description here

However, I don't think you'd really want to use \quad, or similar manual spacing, to write algorithms, it would be better to use one of the several existing LaTeX packages for that purpose.

See Werner's answer at https://tex.stackexchange.com/a/64354/586 for an example. To use this in LyX, go first to Document --> Settings --> LaTeX preamble and add

\usepackage{algorithm}
\usepackage{algpseudocode}

In your document, add an ERT with Ctrl + L or Insert --> TeX code, and write the entire algorithm environment in this, e.g.

\begin{algorithm}
\caption{Euclid's algorithm}\label{euclid} 
\begin{algorithmic}[1] 
\Procedure{Euclid}{$a,b$}\Comment{The g.c.d. of a and b} 
\State $r\gets a\bmod b$ 
\While{$r\not=0$}\Comment{We have the answer if r is 0} 
\State $a\gets b$ 
\State $b\gets r$ 
\State $r\gets a\bmod b$ 
\EndWhile\label{euclidendwhile} 
\State \textbf{return} $b$\Comment{The gcd is b} 
\EndProcedure 
\end{algorithmic} 
\end{algorithm} 

enter image description here

The ERT requirement is a drawback, but if you can get https://tex.stackexchange.com/a/115729/586 to work then that can be avoided.

Also, if you are content with letting your algorithms be floats (similar to figures and tables), you can modify the above example a little. First, remove \usepackage{algorithm} from the preamble. Add an algorithm float with Insert --> Float --> Algorithm. This adds the caption box automatically, and you can add the label as normal. Insert an ERT after the caption, and write the algorithmic environment in this, i.e. just

\begin{algorithmic} 
...
\end{algorithmic} 

enter image description here

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.