减少 LaTex 中算法的边界线

减少 LaTex 中算法的边界线

我正在尝试在 overleaf 上的单列乳胶文档中包含一个居中算法,但是从图中可以看出,边框需要缩短宽度才能看起来更适合页面。

如何减少顶部和底部线条的宽度,使它们仅覆盖文本,但仍保持在页面中央

\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}
\renewcommand{\headrulewidth}{0pt}
\rhead{\large{\textit{header
}}}
\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
\large{\textit{No emails (please)}}
\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



\renewcommand{\sfdefault}{\large{\textbf{1 \quad \quad INTRODUCTION}}}
\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}


\renewcommand{\sfdefault}{\large{\textbf{2 \quad \quad INTRODUCTION2}}}
\sfdefault

\end{flushleft}

\renewcommand{\sfdefault}{\large{\textbf{2 \quad \quad REFERENCE}}}
\sfdefault
\bibliographystyle{apacite}
\bibliography{sa.bib}



\end{document}

输出

答案1

一种可能的方法是将algorithm环境置于minipage,例如将算法放入小页面. minipage 允许指定宽度,例如0.5\textwidth

请注意,这algorithm是一个浮动环境,因此它不能直接在小页面中使用,而只能在使用参数禁用浮动时使用[H](即 [H]ere)。

为了使算法位于页面的中心,您可以将其放在center环境中。

梅威瑟:

\documentclass{article}
\usepackage{fancyhdr}

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


\begin{document}
\pagestyle{fancy}
\rhead{\textit{header}}

\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}

结果:

在此处输入图片描述

答案2

如果您希望限制算法的宽度,最简单的选择是将其放置在minipage使用预先指定的宽度并将其放置在center环境中(使用环境[H]的浮点说明符) 。如果您愿意,algorithm您仍然可以通过将整个构造放置在里面来使其浮动figure

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

内部\caption将按algorithm预期工作,仅使用figure环境作为浮动机制。

如果你想找到算法的真实(自然)宽度,你可以使用varwidth同名的环境varwidth来捕获环境内容的宽度algorithmic

在此处输入图片描述

\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}

相关内容