"Problem definition" environment

You can create an unbreakable structure that would set the question:

enter image description here

\documentclass{article}
\usepackage{tabularx,lipsum,environ,amsmath,amssymb}

\makeatletter
\newcommand{\problemtitle}[1]{\gdef\@problemtitle{#1}}% Store problem title
\newcommand{\probleminput}[1]{\gdef\@probleminput{#1}}% Store problem input
\newcommand{\problemquestion}[1]{\gdef\@problemquestion{#1}}% Store problem question
\NewEnviron{problem}{
  \problemtitle{}\probleminput{}\problemquestion{}% Default input is empty
  \BODY% Parse input
  \par\addvspace{.5\baselineskip}
  \noindent
  \begin{tabularx}{\textwidth}{@{\hspace{\parindent}} l X c}
    \multicolumn{2}{@{\hspace{\parindent}}l}{\@problemtitle} \\% Title
    \textbf{Input:} & \@probleminput \\% Input
    \textbf{Question:} & \@problemquestion% Question
  \end{tabularx}
  \par\addvspace{.5\baselineskip}
}
\makeatother

\begin{document}

\lipsum[1]

\begin{problem}
  \problemtitle{$f$-\textsc{Factor}}
  \probleminput{A graph $G = (V,E)$ and a function $f : V \mapsto \mathbb{N}_0$.}
  \problemquestion{Is there an \emph{$f$-factor}, that is, a subgraph $G' = (V,E')$ of~$G$ such that
    $\text{deg}_{G'}(v) = f(v)$ for all $v \in V$?}
\end{problem}

\lipsum[2]

\end{document}

The problem environment requests you to use \problemtitle, \probleminput and \problemquestion (in any order) to set the title, input and question statement inside a tabularx.


I wanted precisely what you asked, but framed, and with the opportunity of adding several rows to the table, for example Input, Output, or Instance and Task.

I wrote a solution starting from Werner's code using only tabularx which can produce different looking environments. The environment has an optional argument that can be one of

  • (empty or omitted) no framing around the environment
  • framed a single frame around the environment
  • lined two single horizontal lines above and below the environment
  • doublelined two double horizontal lines above and below the environment

You can type any number of lines with any boldface item on the left:

\begin{problem}[framed]{Title of my problem}
  Task: & Define a new ``problem'' environment. \\
  Problem: & The only keywords you can use to search on Google
    are ``latex'' ``problem'' ``definition'' ``environment''. \\
  Solution: & Be patient.
\end{problem}

Example of problem environment

Most of the code is deciding whether the optional argument is empty, framed, lined, or doublelined. Credit to Joseph's answer for being able to use a macro inside the column definition of tabularx. Originally I had written an implementation that relied only on tabularx, but that conflicted with some packages (among which biblatex).

\usepackage{tabularx, environ}

\makeatletter

% https://tex.stackexchange.com/a/199244/26355
\newcolumntype{\expand}{}
\long\@namedef{NC@rewrite@\string\expand}{\expandafter\NC@find}

\NewEnviron{problem}[2][]{%
  \def\problem@arg{#1}%
  \def\problem@framed{framed}%
  \def\problem@lined{lined}%
  \def\problem@doublelined{doublelined}%
  \ifx\problem@arg\@empty%
    \def\problem@hline{}%
  \else%
    \ifx\problem@arg\problem@doublelined%
      \def\problem@hline{\hline\hline}%
    \else%
      \def\problem@hline{\hline}%
    \fi%
  \fi%
  \ifx\problem@arg\problem@framed%
    \def\problem@tablelayout{|>{\bfseries}lX|c}%
    \def\problem@title{\multicolumn{2}{|l|}{%
        \raisebox{-\fboxsep}{\textsc{\Large #2}}%
      }}%
  \else
    \def\problem@tablelayout{>{\bfseries}lXc}%
    \def\problem@title{\multicolumn{2}{l}{%
        \raisebox{-\fboxsep}{\textsc{\Large #2}}%
      }}%
  \fi%
  \bigskip\par\noindent%
  \renewcommand{\arraystretch}{1.2}%
  \begin{tabularx}{\textwidth}{\expand\problem@tablelayout}%
    \problem@hline%
    \problem@title\\[2\fboxsep]%
    \BODY\\\problem@hline%
  \end{tabularx}%
  \medskip\par%
}
\makeatother

Tags:

Environments