7

I am writing a code using the algorithm package. However, I would like to put smaller edges. I used the minipage, boxes and did not work. Could someone give me a suggestion?

I have this

\documentclass[a4paper]{article}
\usepackage[english]{babel}
\usepackage[utf8]{inputenc}
\usepackage{amsmath} 
\usepackage{algorithm}
%========================
\begin{document}  
 \section*{Example Algorithm} 
 %---------------------- 
   \begin{algorithm}
   \caption{Penalty function}   
     \begin{itemize}
        \item Let $\{c_k\}$, $k=1,2,\ldots$, be a sequence tending to infinity such that for each $k$, $0 \leq c_k < c_{k+1}$.  
        \item Define the function $$q(\mu_{k},x) = f(x) + \mu_{k}P(x).$$
        \item For each k solve the problem        
              \begin{gather*}  minimize \ q(\mu_{k},x), \end{gather*}
             obtaining a solution point $x_{k}$.
     \end{itemize}
   \end{algorithm} 
 %----------------------
\end{document}

which results

enter image description here

but, I want

enter image description here

  • A solution for putting a floating environment into minipage is here and it works for this problem. You just have to add [H] behind \begin{algorithm} and it works in the minipage. – dexteritas Jan 25 '17 at 19:29
8

The algorithm float is defined using float's \newfloat. As a result, the entire float is consumed for possible restructuring (including the placement of the \caption, which depends on the style). This also affects the possibility of adjusting the width of the float.

One way around this would be to place the float inside a minipage and make it not float. This is supported by the float package's [H] float specification:

enter image description here

\documentclass{article}

\usepackage{amsmath,algorithm}

\begin{document}  

\section*{Example Algorithm} 

\begin{algorithm}[H]
  \caption{Penalty function}
  \begin{itemize}
    \item Let $\{c_k\}$, $k = 1,2,\dots$, be a sequence tending to infinity such that for each~$k$, $0 \leq c_k < c_{k+1}$.  
    \item Define the function $$q(\mu_{k},x) = f(x) + \mu_k P(x).$$
    \item For each k solve the problem        
          \[ \text{minimize } q(\mu_k,x),c \]
          obtaining a solution point~$x_k$.
  \end{itemize}
\end{algorithm}

{\centering
\begin{minipage}{.7\linewidth}
  \begin{algorithm}[H]
    \caption{Penalty function}
    \begin{itemize}
      \item Let $\{c_k\}$, $k = 1,2,\dots$, be a sequence tending to infinity such that for each~$k$, $0 \leq c_k < c_{k+1}$.  
      \item Define the function $$q(\mu_{k},x) = f(x) + \mu_k P(x).$$
      \item For each k solve the problem        
            \[ \text{minimize } q(\mu_k,x),c \]
            obtaining a solution point~$x_k$.
    \end{itemize}
  \end{algorithm}
\end{minipage}
\par
}

\end{document}

Of course, using a minipage requires one to use \begin{algorithm}[H] (to avoid the floating behaviour). If you still want a floating algorithm, you can use the following work-around:

\begin{figure}[htb]
  \centering
  \begin{minipage}{.7\linewidth}
    \begin{algorithm}[H]
      % <your algorithm>
    \end{algorithm}
  \end{minipage}
\end{figure}

We place the non-float algorithm inside a floating figure without using a \caption for the figure.

  • Thanks @Werner. I preferred to use the figure environment because this style is the best for my text. – Lara Jan 25 '17 at 22:57
-3

create another latex file, write down the algorithm, compile it and take a screen shot.

  • 2
    Welcome to TeX.SX! This does not really answer the question as it would make something scalable a raster graphic. – TeXnician Dec 6 '17 at 5:15

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.