2

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?

1
  • Please tell us how or where \WTSATC is defined.
    – Mico
    May 18 at 14:27

2 Answers 2

3

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

2

Here's a solution that automatically sets the overall width of the tabular-like environment to \textwidth-2cm and centers the table within the textblock. There's no need (or even justification for) \resizebox, as line breaking is enabled for cells in the right hand column.

A separate comment: Unless you like the idea of doing the typographical equivalent of shouting at your readers, I don't think it's necessary to embolden the words "Input" and "Question".

The first horizontal line in the following screenshot is there merely to illustrate the width of the text block.

enter image description here

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

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.