如何创建一个看起来像算法的表?

如何创建一个看起来像算法的表?

有谁知道如何创建一个看起来像

enter image description here

这样做的原因是,我要包括一个元程序/伪代码看起来像算法,但不是实际的算法,即没有明确定义的循环等,并且不是使用它作为算法引用\autoref,而是想将它作为表引用,最好称之为表 1,而不是算法 1。

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{amsthm}
\usepackage{amsmath}
\usepackage{thmtools}       
\setlength\parindent{0pt}
\usepackage[linesnumbered,ruled]{algorithm2e}
\usepackage{hyperref}

\begin{document}


\section{Introduction}

{\LinesNumberedHidden
    \begin{algorithm}
        \SetKwInOut{Input}{Input}
        \SetKwInOut{Output}{Output}
        \SetAlgorithmName{Algorithm}{} 

        Initialize: $x^0$ = 0;
        \begin{enumerate}   
            \item Pour hot coal on $f(x)$
            \item Minimize $f(x)$
            \item Sketch $f(x)$ in Paint
        \end{enumerate}
        \caption{Meta-Coal Algorithm}
        \label{table:Coal Meta-Heuristic}
\end{algorithm}}


\bibliographystyle{plain}
\bibliography{references}
\end{document}

答案1

对于不太复杂的算法来说,使用普通表格就非常简单了。

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{amsthm}
\usepackage{amsmath}
\usepackage{thmtools}       
\usepackage{enumitem}
\usepackage[linesnumbered,ruled]{algorithm2e}
\usepackage{hyperref}

\usepackage{array}

\usepackage{booktabs}
\newcolumntype{L}[1]{>{\raggedright\arraybackslash}p{#1}}

\setlength\parindent{0em}

\begin{document}


\section{Introduction}

\begin{table}
  \stepcounter{table}
  \renewcommand{\arraystretch}{1.2}
  \begin{tabular}{*{1}{@{}L{10cm}}}
    \toprule
    \multicolumn{1}{@{}l}{\bfseries Algorithm \thetable\quad Meta - Coal Algorithm} \tabularnewline
    \bottomrule
    Initialize: $x^0$ = 0; \tabularnewline
    \begin{enumerate}[topsep=0pt]   
    \item Pour hot coal on $f(x)$
    \item Minimize $f(x)$
    \item Sketch $f(x)$ in Paint
    \end{enumerate}  
    \bottomrule
    \end{tabular}
    \addtocounter{table}{-1}
    \caption{Meta-Coal Algorithm}
    \label{table:Coal Meta-Heuristic}
\end{table}

See \autoref{table:Coal Meta-Heuristic}


\end{document}

enter image description here

答案2

您可以定义一个ruledtable新的浮点数,基于tabularx

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage[showframe]{geometry}
\usepackage{amsthm}
\usepackage{amsmath}
\usepackage{thmtools}
\setlength\parindent{0pt}
\usepackage{float, caption, tabularx, enumitem, xpatch}
\usepackage[linesnumbered,ruled]{algorithm2e}
\floatstyle{ruled}
\newfloat{ruledtable}{htbp}{lot}%
\floatname{ruledtable}{Table}
\makeatletter
\let\c@ruledtable\c@table% ruledtables use the table counter
\makeatother

\usepackage{hyperref}
\begin{document}


\section{Introduction}


\begin{ruledtable}[!htb]
\captionsetup{labelsep=colon}
        \caption{Meta-Coal Algorithm}
        \label{table:Coal Meta-Heuristic}
\begin{tabularx}{\linewidth}{@{}X@{}}
        Initialize: $x^0$ = 0;
        \begin{enumerate}[after=\vspace*{-\topsep}]
            \item Pour hot coal on $f(x)$
            \item Minimize $f(x)$
            \item Sketch $f(x)$ in Paint
        \end{enumerate}
\end{tabularx}
\end{ruledtable}

\end{document} 

enter image description here

答案3

我要抛开以下几点algorithm2e

\documentclass{article}
\usepackage{algorithm}
\usepackage{algorithmic}

\begin{document}


\section{Introduction}

\begin{algorithm}
\caption{Meta-Coal Algorithm}
\label{table:Coal Meta-Heuristic}
\begin{algorithmic}
\STATE Initialize: $x^0$ = 0;
       \begin{enumerate}
       \item Pour hot coal on $f(x)$
       \item Minimize $f(x)$
       \item Sketch $f(x)$ in Paint
       \end{enumerate}
\end{algorithmic}
\end{algorithm}

\end{document}

如果你想使用越野车algorithm2e,你可以这样做

\documentclass{article}
\usepackage[ruled]{algorithm2e}

\begin{document}


\section{Introduction}

\begin{algorithm}
\caption{Meta-Coal Algorithm}
\label{table:Coal Meta-Heuristic}
Initialize: $x^0$ = 0; \\
\begin{enumerate}
\item Pour hot coal on $f(x)$
\item Minimize $f(x)$
\item Sketch $f(x)$ in Paint
\end{enumerate}
\end{algorithm}

\end{document}

并忍受Overfull \hbox所收到的虚假信息。

enter image description here

相关内容