4

enter image description here

I want to type the above algorithm in beamer block, but I stuck inserting mathcal symbols. I tried $\mathcal{Q}$ and \mathcal{Q}, but both are failed to return expected symbol.

My MWE:

\documentclass{beamer}
\usepackage{listings}
\usetheme{Madrid}
%\lstset{language=Python}

\begin{document}
\begin{frame}[fragile]
\begin{block}{Algorithm}
\begin{lstlisting}
$\mathcal{Q}$
  for i in range(10):
      foo(arg1, arg2)
  bar = qux()
\end{lstlisting}
\end{block}
\end{frame}
\end{document}

Update

\documentclass{beamer}
\usetheme{Madrid}
\usepackage{algpseudocode}
%this code is from: https://tex.stackexchange.com/a/353165/101651
\algnewcommand\algorithmicinput{\textbf{Input:}}
\algnewcommand\algorithmicoutput{\textbf{Output:}}
\algnewcommand\Input{\item[\algorithmicinput]}%
\algnewcommand\Output{\item[\algorithmicoutput]}%

\begin{document}
\begin{frame}[fragile]
    \begin{block}{Algorithm}
        \begin{algorithmic}
      Input: $\mathcal{Q}_{\text{init}}$, \mathcal{A}, and \textit{f}(c).
    Initialize: Obtain \writetilde{c_{i}} by solving frac{\delta f(c)}{\delta c_{i}}=0, for i\in\mathcal{N}.Set k = 1, \mathcal{B} = \mathcal{Q}_{init},\mathcal{u}_{i}=\gamma_{ub}(\mathcal{Q}_{init} and \mathcal{l}_{1} = \gamma_\left\{ lb}(\]mathcal{Q}_{init}.
      Check the feasibility of problem (17) with given \writetilde{c}.
      if feasible then
      c_{0} = \writetilde{c};
      else
      while u_{k} - l_{k} > \epsilon do
      Branching:
      \begin{itemize}
        \item Set \mathcal{Q}_{k} = \mathcal{Q}, where \mathcal{Q} satisfies \gamma_\left\{ lb}(\mathcal{Q} = l_{k}.
          \item Split \mathcal{Q} into \mathcal{Q}_{\rm{I}} and \mathcal{Q}_{\rm{II}}, along one of its longest edges.
          \item Update \mathcal{B}_{k+1} = (\mathcal{B}_{k}\{\mathcal{Q}_{k}}) \union (\mathcal{Q}_{\rm{I}}, \mathcal{Q}_{\rm{II}}.
      \end{itemize}
      Bounding:
      \begin{itemize}
        \item Update \mathcal{u}_{k+1} = \min_{\mathcal{Q}\in\mathcal{B}_{k+1}{\gamma_{ub}(\mathcal{Q})}
        \item Update \mathcal{l}_{k+1} = \min_{\mathcal{Q}\in\mathcal{B}_{k+1}{\gamma_{lb}(\mathcal{Q})}
      \end{itemize}
      Set k=k+1;
      end while
      Set c_{0} = c_{min};
      end if
      Output: c_{0}.
        \end{algorithmic}
    \end{block}
\end{frame}
\end{document}

I typed the above Algorithm with numerous errors. I typed this type of algorithm in LaTeX at first. So, if some one give me the expected code editing this code to get the result like this algorithm, then it is very helpful to me.

5
  • 1
    Your code is a listing but your image is an algorithm, see here: en.m.wikibooks.org/wiki/LaTeX/Algorithms
    – CarLaTeX
    Feb 21 '18 at 4:39
  • @CarLaTeX Yes, you are right.
    – alhelal
    Feb 21 '18 at 4:43
  • @CarLaTeX You can give a answer with your assumption.
    – alhelal
    Feb 21 '18 at 4:49
  • @alhelal I noticed you edited your question to add another question. I suggest that you post a new question, instead. Since nobody is notified about edits very few people will see it, and it will probably remain unanswered. I looked quickly at your code and the major problem is that you are using math-mode commands outside of math mode. You will solve most of them by adding a few $...$ here and there. Feb 21 '18 at 11:40
  • @PhelypeOleinik Thank you for your recommendation. I asked a new question.tex.stackexchange.com/questions/416471/…, Now, should I remove the edited portion?
    – alhelal
    Feb 21 '18 at 15:39
6

The problem is that it is a verbatim environment, so everything is verbatim. To make it not verbatim you have to use the option escapeinside:

\lstset{escapeinside={@(}{)@}}

so that any code between @(...)@ is escaped back to LaTeX.

Best practice is to use more than one character for escaping to avoid ambiguity. Also try to choose a combination that will not appear in your code. In python, for example, using :(...): would be a bad idea because of the syntax of the for loop.

enter image description here

\documentclass{beamer}
\usepackage{listings}
\usetheme{Madrid}
%\lstset{language=Python}
\lstset{escapeinside={@(}{)@}}

\begin{document}
\begin{frame}[fragile]
\begin{block}{Algorithm}
\begin{lstlisting}
@($\mathcal{Q}$)@
  for i in range(10):
      foo(arg1, arg2)
  bar = qux()
\end{lstlisting}
\end{block}
\end{frame}
\end{document}
11
  • I like your answer, but if I want to insert @text@?
    – alhelal
    Feb 21 '18 at 4:35
  • The same rules apply. whatever is inside @@ will be parsed by LaTeX. If you give it @text@, then the contents of text will be parsed by normal LaTeX, not in a verbatim context. Note that in my answer I open math mode with $$ inside the @@. Feb 21 '18 at 4:40
  • You don't understand me. I want to get @text@ as output.
    – alhelal
    Feb 21 '18 at 4:42
  • 1
    Oh, sorry. Then you have to change the value of escapeinside. Actually, best practice is to use more than one character for escaping, and choose something that you'll not have in your listing. I'll update my answer with that. Feb 21 '18 at 4:45
  • what is the symbol in 2nd line of my algorithm image (u_1, l_1). I can't make these symbol in latex.
    – alhelal
    Feb 22 '18 at 10:41
3

Since the OP would like to see an example of an algorithm...

The code for \Input comes from an answer of gernot's.

\documentclass{beamer}
\usetheme{Madrid}
\usepackage{algpseudocode}
%this code is from: https://tex.stackexchange.com/a/353165/101651
\algnewcommand\algorithmicinput{\textbf{Input:}}
\algnewcommand\algorithmicoutput{\textbf{Output:}}
\algnewcommand\Input{\item[\algorithmicinput]}%
\algnewcommand\Output{\item[\algorithmicoutput]}%

\begin{document}
\begin{frame}[fragile]
    \begin{block}{Algorithm}
        \begin{algorithmic}
            \Input $\mathcal{Q}_{\text{inn}}$
            \For{$i=0$ to $10$}
            \State do something with arg1 and arg2
            \State $bar \leftarrow qux()$
            \EndFor
        \end{algorithmic}
    \end{block}
\end{frame}
\end{document}

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.