我正在写一篇论文,其中定义了一些计算问题,我希望它们都具有教科书中介绍的通常风格。就像这样:
我定义了一个命令来轻松地完成此操作,将其作为表格,每次我都会在其中输入问题的详细信息。 我的代码如下:
\newcommand{\problemStatement}[3]{
\begin{table}[]
\centering
\resizebox{\textwidth}{!}{%
\begin{tabular}{llrl}
\cmidrule(lr){2-3}\hspace{1cm} & \multicolumn{2}{c}{#1} & \hspace{1cm} \\
\cmidrule(lr){2-3} & \textbf{Input} & #2 & \\
& \textbf{Question} & #3 &
\end{tabular}%
}
\end{table}
}
例如,写作
\problemStatement{\WTSATC}{A 3CNF formula $\varphi$, a partial assignment $\alpha$ and a natural number $k$.}{Is there a satisfying assignment extending $\alpha$ that only sets $k$ more variables to true?}
结果是
但问题是,这个表格的大小每次都会根据其内容而变化。我本质上希望字体与文档中其他主要文本的大小相同,并且表格的宽度始终固定,而不会填满整个文本列。有什么想法可以解决这个问题吗?
答案1
首先,你不想要\resizebox
,但很可能你也不想要table
。
\documentclass{article}
\usepackage{booktabs,tabularx}
\usepackage{lipsum}
\newcommand{\problemStatement}[3]{%
\begin{center}
\begin{tabularx}{\columnwidth}{@{}lX@{}}
\toprule
\multicolumn{2}{@{}c@{}}{\textsc{#1}}\tabularnewline
\midrule
\bfseries Input: & #2 \\
\bfseries Question: & #3 \\
\bottomrule
\end{tabularx}
\end{center}
}
\begin{document}
\lipsum[3]
\problemStatement{Weighted 3CNF SAT Completion}
{A 3CNF formula $\varphi$, a partial assignment $\alpha$ and a natural number~$k$.}
{Is there a satisfying assignment extending $\alpha$ that only sets
$k$ more variables to true?}
\lipsum[4]
\end{document}
这是另一个版本,语法不同,可以容纳包含两个以上项目的不同类型的问题。我在“问题”中添加了一个重音符号,只是为了证明它有效。
\documentclass{article}
\usepackage{booktabs,tabularx,amsmath}
\usepackage{lipsum}
\ExplSyntaxOn
\NewDocumentCommand{\problemStatement}{mm}
{% #1 is the title
% #2 is the contents
\arteche_problemstatement:nn { #1 } { #2 }
}
\prop_new:N \l_arteche_problemstatement_body_prop
\cs_new_protected:Nn \arteche_problemstatement:nn
{
\prop_set_from_keyval:Nn \l_arteche_problemstatement_body_prop { #2 }
\begin{center}
\begin{tabularx}{\columnwidth}{@{}lX@{}}
\toprule
\multicolumn{2}{@{}c@{}}{\textsc{#1}}\tabularnewline
\midrule
\prop_map_function:NN \l_arteche_problemstatement_body_prop \__arteche_problemstatemet_do:nn
\bottomrule
\end{tabularx}
\end{center}
}
\cs_new_protected:Nn \__arteche_problemstatemet_do:nn
{
\bfseries \tl_rescan:nn { } { #1 }: & #2 \\
}
\ExplSyntaxOff
\begin{document}
\lipsum[3]
\problemStatement{Weighted 3CNF SAT Completion}{
Input={A 3CNF formula $\varphi$, a partial assignment $\alpha$ and a natural number~$k$.},
Question={Is there a satisfying assignment extending $\alpha$ that only sets
$k$ more variables to true?}
}
\lipsum[4]
\problemStatement{3-Colorability}{
Instance={A graph $G=(V,E)$},
Parameter={$k=\operatorname{tw}(G)$ the treewidth of $G$},
Quêstion={Is there a mapping $c\colon V \to \{1,2,3\}$ such that
for all $v_1,v_2\in V$, $v_1\ne v_2$, we have $c(v_1)\ne c(v_2)$?}
}
\end{document}
答案2
这是一个解决方案,它会自动将类似表格的环境的整体宽度设置为 ,\textwidth-2cm
并将表格置于文本块的中心。没有必要(甚至没有理由)\resizebox
,因为右侧列的单元格已启用换行功能。
一个单独的评论:除非你喜欢做印刷等同于大喊在你的读者面前,我认为没有必要鼓舞“输入”和“问题”这两个词。
下图中的第一条水平线仅用于说明文本块的宽度。
\documentclass{article} % or some other suitable document class
\usepackage{tabularx,ragged2e,booktabs}
\providecommand{\WTSATC}{WTSATC} % ??
\newlength\mylen
\setlength\mylen{\dimexpr\textwidth-2cm\relax} % overall width
\newcommand{\problemStatement}[3]{%
\begin{center}
\begin{tabularx}{\mylen}{@{} l >{\RaggedRight}X @{}}
\toprule
\multicolumn{2}{@{} >{\Centering}p{\mylen} @{}}{#1} \\
\midrule
Input & #2 \\
\addlinespace
Question & #3 \\
\bottomrule
\end{tabularx}
\end{center}}
\begin{document}
\hrule % just to illustrate width of textblock
\problemStatement{\WTSATC}{%
A 3CNF formula $\varphi$, a partial assignment~$\alpha$ and
a natural number~$k$.}{%
Is there a satisfying assignment extending~$\alpha$ that
only sets $k$ more variables to true?}
\end{document}