7

Is there such a thing as an environment for defining problems? See this picture for an example of what I want to achieve: example problem definition

Thanks!

2
  • Welcome to TeX.SX! amsthm or ntheorem packages perhaps? Your question is not very precise
    – user31729
    Jul 16, 2015 at 17:02
  • 1
    Yeah sorry I'm new to latex. And thank you :) I'm just looking for a way to properly format a problem definition like shown in the pic, where I have a problem title, an input and a question, such that the input and the question are aligned as seen above. I tried the algorithm2e package, but it messes up the alignment.
    – Peter W
    Jul 16, 2015 at 17:16

2 Answers 2

9

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.

2
  • That works just like a charm, thank you very much!
    – Peter W
    Jul 16, 2015 at 17:30
  • Aren’t you missing a closing \makeatother? But yes, works fine :)
    – Max Merz
    Dec 8, 2019 at 13:50
8

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}{|%
      >{\raisebox{-\fboxsep}}%
      p{\dimexpr\textwidth-4\fboxsep-2\arrayrulewidth\relax}%
      |}{%
        \textsc{\Large #2}%
      }}%
  \else
    \def\problem@tablelayout{>{\bfseries}lXc}%
    \def\problem@title{\multicolumn{2}{>%
      {\raisebox{-\fboxsep}}%
      p{\dimexpr\textwidth-4\fboxsep\relax}%
      }{%
        \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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .