LaTeX macro for complexity theory problem statement

complexitymacrostables

I'm writing a thesis where I define some computational problems and I want them all to have the usual style presented in textbooks. Something like this:
enter image description here

I defined a command to do this easily as a table where I input the details of the problem each time. My code is as follows:

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

so that, for example, writing

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

yields something like

enter image description here

Problem, however, is that the size of this table changes every time, depending on its contents. I essentially want the font to be the same size as the rest of the main text in my document, and the widht of the table to always be fixed, without it filling the entire text column. Any ideas how to fix it?

Best Answer

First off, you don't want \resizebox and quite likely you don't want table either.

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

enter image description here

Here's another version, with a different syntax, that can accommodate different types of problems with more than two items. I added an accent in “Question” just to show it works.

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

enter image description here

Related Question