# reduce border line on algorithm in LaTex

I am trying to include a centered algorithm in a single column latex document on overleaf, however as can be seen from the picture, the border requires to be shortend in width to look and fit the page better.

How can I reduce the width of the top and bottom lines, so that they only cover the text, but remain centered on the page

\documentclass{article}
\usepackage[tmargin=1.25in,bmargin=1.25in,lmargin=1.5in,rmargin=1.5in]{geometry}
\usepackage{fancyhdr}

\usepackage[fleqn]{amsmath}
\usepackage{algorithm}
\usepackage{algpseudocode}

\usepackage{apacite}

\begin{document}
\fontfamily{ptm}\selectfont
\pagestyle{fancy}
}}}
\rfoot{Copyright $\textcopyright$ 2018+}
\cfoot{}
\sffamily
\noindent\rule{14cm}{1pt}
\begin{center}
\huge{\textbf{Title}}
\end{center}
\vspace{-\baselineskip}
\noindent\rule{14cm}{1pt}
\fontfamily{ptm}\selectfont
\begin{flushleft}
\vspace{7mm}
\par
\large{\textbf{authors}}
\vspace{5mm}
\par
\large{\textit{School}}
\par
\vspace{7mm}
\par
\renewcommand{\sfdefault}{\large{\textbf{ABSTRACT}}}
\sfdefault
\par
\fontfamily{ptm}\selectfont
abstract.
\vspace{5mm}
\par
\textit{Keyword:}
\par
\vspace{5mm}
\noindent\rule{14cm}{1pt}
\vspace{5mm}
\par

\sfdefault
\par
\fontfamily{ptm}\selectfont
Describe the general perspective of the chapter. Toward the end, specifically state the objectives of the chapter.
\vspace{7mm}
\par
\renewcommand{\sfdefault}{\large{\textbf{BACKGROUND (SUBHEAD STYLE 1- ARIAL, SIZE 12, BOLD)}}}
\sfdefault
\par
\fontfamily{ptm}\selectfont
Provide broad definitions and discussions of the topic and incorporate views of others (literature review) into the discussion to support, refute or demonstrate your position on the topic.
\vspace{7mm}
\par
\renewcommand{\sfdefault}{\large{\textbf{MAIN FOCUS OF THE CHAPTER}}}
\sfdefault
\vspace{7mm}
\par
\cite{TEIXEIRA2017}

\begin{algorithm}
\centering
\caption{title}\label{algorithm}
\begin{algorithmic}[1]
\State \text{algorithm}
\Repeat
\State \text{/*Parse data to format*/}
\For{$i\gets 1, rows$}
\State $\text{algorithm}$
\State $\text{Pad data arrays with 0s}$
\EndFor
\Until {Data is converted}
\State \text{algorithm}
\Repeat
\State \text{algorithm}
\For{$i\gets 1, rows$}
\State $\text{algorithm}$
\EndFor
\Until {algorithm}
\State \textbf{Return}  \text{algorithm}
\end{algorithmic}
\end{algorithm}

\sfdefault

\end{flushleft}

\sfdefault
\bibliographystyle{apacite}
\bibliography{sa.bib}

\end{document}


• Related (although with a different algorithm package): tex.stackexchange.com/questions/215090/…. The idea is that if you put the algorithm in a minipage environment smaller than the full width, then the lines will be smaller as well. – Marijn Jun 20 '18 at 13:36
• @Marijn thanks for suggestion. I tried that but didnt seem to work thanks – Bat Jun 20 '18 at 14:30
• @Bat if you tried it you could put it into the question, saying I tried the following code but it resulted in the following error/the following wrong output: Also, it is good that you provided an MWE, but this code is not really minimal (the M from MWE). For example, the bibliography does not have anything to do with your question, so it should not be in the MWE. The idea is that it is as small as possible, while still a complete document/compilable/working (the W) and still showing your problem (the E). – Marijn Jun 20 '18 at 14:35

A possible approach is to put the algorithm environment in a minipage environment, as in Putting algorithm in minipage. The minipage allows to specify the width, for example 0.5\textwidth.

Note that algorithm is a floating environment, therefore it cannot be used in a minipage directly, but only when floating is disabled with the [H] argument (i.e., [H]ere).

To center the algorithm on the page you can put it in a center environment.

MWE:

\documentclass{article}
\usepackage{fancyhdr}

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

\begin{document}
\pagestyle{fancy}

\begin{center}
\begin{minipage}{0.5\textwidth}
\begin{algorithm}[H]
\centering
\caption{title}\label{algorithm}
\begin{algorithmic}[1]
\State \text{algorithm}
\Repeat
\State \text{/*Parse data to format*/}
\Until {Data is converted}
\end{algorithmic}
\end{algorithm}
\end{minipage}
\end{center}

\end{document}


Result:

• that worked perfectly. I must not have folowed the instructions on the webpage you provided correctly. Many thanks for your help. I have another formatting query I am struggling with, but does not pertain to algorithms - should I start a new questions rather than post here ? – Bat Jun 20 '18 at 20:34
• @Bat you're welcome. If you have another unrelated problem then asking a new question is better - however, maybe the new question is already answered somewhere on the site, so make sure to check that first. – Marijn Jun 20 '18 at 20:37

If you wish to limit the width of the algorithm, your easiest option is to place it in a minipage using a pre-specified width and place it inside a center environment (using the [H] float specifier for the algorithm environment. You can still make it float by placing the entire construction inside a figure, if you wish

\begin{figure}
\centering
\begin{minipage}{<width>}
\begin{algorithm}[H]
\end{algorithm}
\end{minipage}
\end{figure}


The \caption within the algorithm would work as expected, using only the figure environment as a floating mechanism.

If you want to find the true (natural) width of the algorithm, you can use varwidth's similarly-named environment varwidth to capture the width of the algorithmic environment contents:

\documentclass{article}

\usepackage{algorithm}
\usepackage{algpseudocode}

\usepackage{varwidth}
\usepackage{lipsum}

\newsavebox{\algbox}
\algrenewcommand{\Return}{\State \textbf{return}~}

\begin{document}

% Capture the algorithm pseudo-code inside a box; maximum width will be \linewidth
\begin{lrbox}{\algbox}
\begin{varwidth}{\linewidth}
\begin{algorithmic}[1]
\State algorithm
\Repeat
\State /*Parse data to format*/
\For{$i \gets 1, rows$}
\State algorithm
\State Pad data arrays with 0s
\EndFor
\Until Data is converted
\State algorithm
\Repeat
\State algorithm
\For{$i \gets 1, rows$}
\State algorithm
\EndFor
\Until algorithm
\Return algorithm
\end{algorithmic}
\end{varwidth}
\end{lrbox}

\lipsum[1]

\begin{center}% Or use a figure environment and \centering
\begin{minipage}{\wd\algbox}
\begin{algorithm}[H]
\caption{Algorithm title}
\usebox{\algbox}% Use algorithm pseudo-code box
\end{algorithm}
\end{minipage}
\end{center}

\lipsum[2]

\end{document}

• many thanks I went with the minipage suggestion and it worked perfectly ! many thanks for your help ! – Bat Jun 20 '18 at 20:35