2

Does anyone know how to create a table that looks like

enter image description here

The reason for doing this is that I am including a meta-procedure/pseudo-code that looks like algorithm, but not an actual algorithm i.e. no well defined for loops etc. and instead of referencing it as an algorithm using \autoref, I want to reference it as a table instead and preferably call it Table 1, instead of Algorithm 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}
  • Wouldn't it be simpler to have a real table with a special formatting for the caption? – Bernard Feb 24 '17 at 20:09
  • @Bernard Yes, I'm trying to get that working also but without the border on the left and the right – Carlos - the Mongoose - Danger Feb 24 '17 at 20:31
  • What do you mean with ‘the border’? – Bernard Feb 24 '17 at 23:49
1

For not too sophisticated algorithms this is pretty straightforward with a normal table.

\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

  • You are missing a \endrule. – Benjamin McKay Feb 25 '17 at 9:55
  • @BenjaminMcKay: You mean \bottomrule -- \endrule is rather unlikely, unless defined with \def ;-) – user31729 Feb 25 '17 at 10:14
1

You can define a ruledtable new float, baed on 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

1

I would leave aside 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}

If you want to use the buggy algorithm2e, you can do

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

and live with the spurious Overfull \hbox message you get.

enter image description here

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.