带有标题和对齐方式的盒装环境

带有标题和对齐方式的盒装环境

我希望实现类似于这些用于描述问题的漂亮描述性框样式组合优化:理论与算法作者:Jens Vygen 等人

在此处输入图片描述

该框的长度为\textwidth。我发现 fancybox 可以做类似的事情,但我无法正确添加标题或将文本对齐成两个部分。有没有好的包可以实现类似的功能?

答案1

这是一个相当原始的版本tabularx

\documentclass{article}

\usepackage{tabularx}
\usepackage{blindtext}

\newcommand{\myboxstuff}[3]{%
  \noindent%
  \begin{tabularx}{\linewidth}{|llX|}
    \hline
    \multicolumn{3}{|X|}{\scshape #1} \tabularnewline
    & & \tabularnewline
    \textit{Instance:} & & #2 \tabularnewline
    & & \tabularnewline
    \textit{Task:} & & #3 \tabularnewline
    \hline
  \end{tabularx}%
}

\begin{document}

\myboxstuff{Linear Programming}{Some Matrix\dots}{\blindtext}

\end{document}

在此处输入图片描述

答案2

这里有两个解决方案,分为两种变体:一种也基于tabularx,另一种基于framedenumitem定义一个programdescr可以跨页中断的环境。定义了两个命令:\instancetask

    \documentclass[11pt]{article}
    \usepackage[utf8]{inputenc}
\usepackage[showframe]{geometry} \usepackage{MinionPro}
    \usepackage{tabularx, makecell}
    \setcellgapes{4pt}
    \usepackage{enumitem, framed}
\usepackage{etoolbox, xcolor} %


\newenvironment{programdescr}[1][]{\FrameSep=6pt\framed%
\setlist[description, 1]{font=\normalfont\itshape, , wide = 0pt, labelwidth=1.6cm, leftmargin=\labelwidth, itemindent=0pt, style=sameline}%
\newcommand\instance{\item[Instance:]}\newcommand\task{\item[Task:]}%
\noindent\textsc{#1}\vspace{-0.5\topsep}\description}%
{\enddescription\vspace{-\topsep}\endframed}%

    \begin{document}

    \makegapedcells
    \noindent \begin{tabularx}{\linewidth}{|>{\itshape}p{1.2cm} >{\arraybackslash}X |}
    \hline
    \multicolumn{2}{|>{\scshape}l|}{Linear Programming} \\ Instance: & A matrix $ A \in \mathbf R^{m \times n} $ and column vectors $ b \in \mathbf R^m $, $ c \in \mathbf R^n$. \\%
    Task: & Find a column vector $ b \in \mathbf R^n $ such that $ Ax \le b $ and $ c^\top x $ is maximum, decide that $ \{x \in \mathbf R^n\:\colon\: Ax \le b \} $ is empty, or decide that for all $ \alpha  \in \mathbf R $ there is an $ x \in \mathbf R^n$ with $ Ax \le b$ and $ c^\top x > \alpha $. \\%
    \hline
    \end{tabularx}
    \vskip1cm
    \noindent \begin{tabularx}{\linewidth}{|>{\itshape\raggedleft}p{1.2cm}>{\arraybackslash}X |}
    \hline
    \multicolumn{2}{|>{\scshape}l|}{Linear Programming} \\ Instance: & A matrix $ A \in \mathbf R^{m \times n} $ and column vectors $ b \in \mathbf R^m $, $ c \in \mathbf R^n$. \\%
    Task: & Find a column vector $ b \in \mathbf R^n $ such that $ Ax \le b $ and $ c^\top x $ is maximum, decide that $ \{x \in \mathbf R^n\:\colon\: Ax \le b \} $ is empty, or decide that for all $ \alpha  \in \mathbf R $ there is an $ x \in \mathbf R^n$ with $ Ax \le b$ and $ c^\top x > \alpha $. \\%
    \hline
    \end{tabularx}

    \FrameSep=6pt\begin{framed}
    \noindent\textsc{Linear Programming}\vspace{-0.5\topsep}
      \begin{description}[font=\normalfont\itshape, labelwidth=1.6cm, leftmargin=\labelwidth, itemindent=0pt, align=right]

     \item[Instance:]
     A matrix $ A \in \mathbf R^{m \times n} $ and column vectors $ b \in \mathbf R^m $, $ c \in \mathbf R^n$.

    \item[Task: ] Find a column vector $ b \in \mathbf R^n $ such that $ Ax \le b $ and $ c^\top x $ is maximum, decide that $ \{x \in \mathbf R^n\:\colon\: Ax \le b \} $ is empty, or decide that for all $ \alpha  \in \mathbf R $ there is an $ x \in \mathbf R^n$ with $ Ax \le b$ and $ c^\top x > \alpha $.

        \end{description}
    \vspace{-\topsep}
    \end{framed}

    \begin{programdescr}[Linear Programming]
    \instance
     A matrix $ A \in \mathbf R^{m \times n} $ and column vectors $ b \in \mathbf R^m $, $ c \in \mathbf R^n$.

    \task Find a column vector $ b \in \mathbf R^n $ such that $ Ax \le b $ and $ c^\top x $ is maximum, decide that $ \{x \in \mathbf R^n\:\colon\: Ax \le b \} $ is empty, or decide that for all $ \alpha  \in \mathbf R $ there is an $ x \in \mathbf R^n$ with $ Ax \le b$ and $ c^\top x > \alpha $.

    \end{programdescr}

    \end{document} 

在此处输入图片描述

相关内容