2

So I'm not experienced with the algorithm packages in LaTeX. I'm assuming there must be a way to do

if condition1 and condition2 then...

but I can't find it. I've checked what I can find of the documentation. If I compile the below I get an error for undefined command at the \And. It looks like there is something in algorithm, using \AND, but that doesn't work here and I don't really understand what's going on.

\documentclass{article}
\usepackage[noend]{algpseudocode}
\usepackage[nothing]{algorithm}
\algrenewcommand{\algorithmicrequire}{\textbf{Input:}}
\algrenewcommand{\algorithmicensure}{\textbf{Output:}}

\begin{document}

\begin{figure}
\begin{algorithmic}
\If{$x> y$ \And $ x<z$}
\State{some code here}
\EndIf
\end{algorithmic}
\end{figure}
\end{document}

Is there a way to do what I'm after?

3

I could not find a \And command but we can make one:

\algnewcommand\And{\textbf{and}}

and add some space...

\documentclass{article}
\usepackage[noend]{algpseudocode}
\usepackage[nothing]{algorithm}
\algrenewcommand{\algorithmicrequire}{\textbf{Input:}}
\algrenewcommand{\algorithmicensure}{\textbf{Output:}}
\algnewcommand\And{\textbf{and} }

\begin{document}

\begin{figure}
\begin{algorithmic}
\If{$x> y$ \And  $ x<z$}
\State{some code here}
\EndIf
\end{algorithmic}
\end{figure}
\end{document}

Thanks: @Werner

  • 2
    And what is the difference? Did you see the second par of the question? – percusse Mar 23 '15 at 19:25
  • Sorry I was confused with other algorithms package. – JJoao Mar 23 '15 at 19:35
  • Note the use here should be \And{}, or define it to include a trailing space: \algnewcommand\And{\textbf{and} }. – Werner Mar 23 '15 at 19:37
  • @Werner, Thank you! (sorry I was editing and I just saw your suggestion). I don't know: perhaps your solution is better... – JJoao Mar 23 '15 at 19:40
  • 1
    @JJoao: You would most likely not use \And unless you have two operands (one on either side), so it seems logical to define \And to always include a space. xspace is a viable option, noting of course that there might be some drawbacks of using xspace. – Werner Mar 23 '15 at 19:44
1

I suggest use the answer by Werner if you use the algorithmic package. Link: Using the "algorithm" package - can't use \OR?

enter image description here

\documentclass{article}

\usepackage{algpseudocode}

\algnewcommand{\algorithmicand}{\textbf{ and }}
\algnewcommand{\algorithmicor}{\textbf{ or }}
\algnewcommand{\OR}{\algorithmicor}
\algnewcommand{\AND}{\algorithmicand}
\algnewcommand{\var}{\texttt}

\begin{document}

\begin{algorithmic}
  \Procedure{proc}{$v$}
    \State $\var{v.bool} \gets \var{v.bool} \OR \var{c.bool}$;
  \EndProcedure
\end{algorithmic}

\end{document}
New contributor
Kevin Lee is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

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.