是否存在定义问题的环境?请参阅此图,了解我想要实现的目标:
谢谢!
答案1
你可以创建一个牢不可破的结构来提出以下问题:
\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}
环境problem
要求您使用\problemtitle
、\probleminput
和\problemquestion
(以任何顺序)在tabularx
。
答案2
我想要的正是你所要求的,但是框架,并有机会在表格中添加几行,例如输入,输出, 或者实例和任务。
我使用仅能产生不同外观环境的 Werner 代码编写了一个解决方案tabularx
。环境有一个可选参数,可以是以下之一:
- (空或省略)环境周围无框架
framed
围绕环境的单一框架lined
环境上方和下方的两条水平线doublelined
环境上方和下方的两条双水平线
您可以输入任意数量的行,并在左侧输入任意粗体项:
\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}
大部分代码都在决定可选参数是否为空、、framed
或lined
。doublelined
归功于约瑟夫的回答因为能够在 的列定义中使用宏tabularx
。最初,我编写了一个仅依赖于 的实现tabularx
,但它与某些包(其中包括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