Why is my algorithm nowhere to be found in the PDF?

1,201

On the basis of the long string of comments to your question, it seems that the code in your question is still not reflecting what you use on your machine. It's important that you give us all the relevant information (but no more than that; see minimal working example) in one go, instead of doling it out. Otherwise, people who want to help you are just fumbling in the dark for what the problem might be, and are likely to run out of patience quickly.

Here are my thoughts so far. In your question, you write

[the algorithm] crosses two columns, is it possible to make it only occupy one column?

However, whether your document is globally or only locally multicolumn is unclear, because in your comment, you write

i use \begin{multicols}{2}

but, in your question, you also write

my article is two column style

As stated in this article by the author of the multicol package, the multicol environment is only meant for

[...] switching between one and multicolumn format on the same page.

Therefore, if your document is entirely two-column, you don't need any multicol environment. You just need to pass the twocolumn option (along with possibly other options) to the article class:

\documentclass[11pt,twocolumn]{article}

I'm going to assume that's what you want, here. In that case, the algorithm gets typeset as expected in one column with the following code. The only problem is that you may want to keep the lines short, to avoid too many line breaks within your algorithm; consider getting rid of the algorithm comments, for instance, as I did in my code.

enter image description here

\documentclass[11pt,twocolumn]{article}

\usepackage{amsmath}
\usepackage{algpseudocode}
\usepackage{algorithm}
%\usepackage[showframe]{geometry}%<--- only to show the page structure

\begin{document}
\begin{algorithm}
\caption{Euclid’s algorithm}\label{euclid}
\begin{algorithmic}[1]
  \Procedure{Euclid}{$a,b$}
  \State $r\gets a\bmod b$
  \While{$r\not=0$}
  \State $a\gets b$
  \State $b\gets r$
  \State $r\gets a\bmod b$
  \EndWhile\label{euclidendwhile}
  \State \textbf{return} $b$
 \EndProcedure
\end{algorithmic}
\end{algorithm}

\end{document}

If, on the other hand, you really want your document to be only locally two-column, simply wrapping a multicols environment around the algorithm environement (which I believe is what you did) won't work, because algorithm is a "float", and the multicol package doesn't allow "column floats" (as explained, for instance, in egreg's answer to Getting floating objects in multicol environments). For alternatives, see How do I force TeX to completely use the 1st column?

Share:
1,201

Related videos on Youtube

user3601704
Author by

user3601704

Updated on August 10, 2020

Comments

  • user3601704
    user3601704 about 3 years

    I need to typeset a pseudocode algorithm. (I use TexStudio on Windows 7.) If I put the code in a single new tex file, I can see the pdf file. But, I cannot see it if I put it inside one of the other tex files. I have many tex files that cannot be shown here. But, the other parts can be displayed well, only the algorithm part disappear.

    \documentclass[11pt]{article}
    \usepackage{amsmath}
    \usepackage{algpseudocode}
    \usepackage{algorithm}
    
    \begin{document}
    
    \begin{algorithm*}  **// add star here** 
    \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*}
    
    \end{document}
    

    Compilation works fine (I get no errors), but the algorithm is nowhere to be found in the PDF file. To repeat, I use the following packages:

     \usepackage{amsmath}
     \usepackage{algpseudocode}
     \usepackage{algorithm}
    

    If I put the algorithm part in this way :

    mark1 
      "the algorithm part"
    

    mark2

    UPDATE

    The output :

      ----------------------------------
      caption 
      -----------------------------------         
      the algorithm, but , it crosses two columns , is it possible to make it only occupy one column, my article is two column style. 
    

    I can see the "mark1" and "mark2" in pdf but not the "the algorithm part".

    Any idea?

    • jub0bs
      jub0bs about 9 years
      Please make your code compilable (if possible), or at least complete it with \documentclass{...}, the required \usepackage's, \begin{document}, and \end{document}. That may seem tedious to you, but think of the extra work it represents for TeX.SX users willing to give you a hand. Help them help you: remove that one hurdle between you and a solution to your problem.
    • Gonzalo Medina
      Gonzalo Medina about 9 years
      Perhaps the algorithm is there, but on a different page. Notice that algorithm is a float object, so your algorithm might be in a different page than the one you wrote the code in.
    • greyshade
      greyshade about 9 years
      Are you sure you're showing us everything that's relevant? Taking your code and putting it into an MWE works perfectly fine for me.
    • user3601704
      user3601704 about 9 years
      @GonzaloMedina, I have checked that no algorithm in pdf file. thanks
    • greyshade
      greyshade about 9 years
      @user3601704 With the updated MWE, do you get the algorithm appearing in the .pdf?
    • hola
      hola about 9 years
      @user3601704 Try manually compiling the code.
    • hola
      hola about 9 years
      @user3601704 One more thing, which PDF viewer are you using?
    • hola
      hola about 9 years
      @user3601704 May be, your compiled PDF contains the algorithm, but you are viewing a separate one!
    • user3601704
      user3601704 about 9 years
      I use viewPDF. How to do manual compile ? Where to find the pdf that have the algorithm ? thanks
    • greyshade
      greyshade about 9 years
      Try pdflatex <nameofyourfile> from the command line (cd to the folder you're working in first..) and you should find a pdf of the same name afterwards.
    • user3601704
      user3601704 about 9 years
      i used pdflatex from cmd and compiled the tex file, no compile error, in the folder, i opened the pdf file, but still no algorithm in the pdf file.
    • greyshade
      greyshade about 9 years
      @user3601704 And the contents of your .tex file were exactly those of the updated MWE that's now in the question`?
    • user3601704
      user3601704 about 9 years
      No, there are also other texts, But, I can see the pdf even though I do not put the "algorithm" part in the file. So, the other parts do not have impacts on the pdf. But, why I cannot see the "algorithm" in pdf ? thanks
    • egreg
      egreg about 9 years
      @user3601704 Missing text can be caused by a misplaced \endinput or \end{document}.
    • jub0bs
      jub0bs about 9 years
      @user3601704 Let's see whether it's a float problem: try removing the surrounding algorithm environment and the caption (but leaving the algorithmic one). What do you see in the PDF then?
    • user3601704
      user3601704 about 9 years
      @Jubobs, after removing them, I see the "algorithm" part in pdf, but, it cross two columns. Can we reduce it to one column ? Now, I use \begin{algorithm*}, it is correct way ? thanks !
    • jub0bs
      jub0bs about 9 years
      @user3601704 Good; that's progress. Please edit the code in your question to show us exactly with what code you're working at the moment, and what output you get. Otherwise, we won't get anywhere.
    • jub0bs
      jub0bs about 9 years
      @user3601704 Just to clarify, can you confirm whether you can see the algorithm now? Also, if you document is supposed to be two-column, why are you not using the twocolumn class option?
    • user3601704
      user3601704 about 9 years
      @Jubobs, i use \begin{multicols}{2}. But, the "algorithm" part cross two columns.
    • jub0bs
      jub0bs about 9 years
      @user3601704 ... you say you use a multicols environment, but it's not in the code of your question! The code in your question must be complete, i.e. it must exactly reflect the minimal working example (MWE) you use on your machine to try and solve this problem. Otherwise, you're not giving us all the information we need. That's why we're not getting anywhere here: you're doling out information instead of telling us everything.