问题解决方案模板

问题解决方案模板

我想要创建一个如下所示的 Latex 文档:

1. This is the problem statement in 
multiple line.

Solution:
This is the solution of the problem. When there is only on
solution, there is no need to numbering

2. This is another problem but two solution
multiple line.

Solution 1:
This is the first solution of the problem

Solution 2:
This is the second solution.

你有什么建议?

PS:我对TeX环境不太熟悉。

答案1

我制作了一个格式,如您的问题中所示。显然,可以对定义进行修饰,以获得更美观的格式。已编辑,以提供提问者所需的使用语法。

\documentclass{article}

\newcounter{problem}
\newcounter{solution}

\newcommand\Problem{%
  \stepcounter{problem}%
  \textbf{\theproblem.}~%
  \setcounter{solution}{0}%
}

\newcommand\TheSolution{%
  \textbf{Solution:}\\%
}

\newcommand\ASolution{%
  \stepcounter{solution}%
  \textbf{Solution \thesolution:}\\%
}
\parindent 0in
\parskip 1em
\begin{document}
\Problem This is the problem statement in multiple line. This is the
problem statement in multiple line. This is the problem statement in
multiple line.

\TheSolution This is the solution of the problem. When there is only on
solution, there is no need to numbering.

\Problem This is another problem but two solution multiple line. This is
another problem but two solution multiple line. This is another problem
but two solution multiple line.

\ASolution This is the first solution of the problem

\ASolution This is the second solution.

\end{document}

在此处输入图片描述

答案2

根据 Steven 的解决方案,我有这个想法,通过定义一个新命令,以 \par 分隔,包装先前在 Steven 的帖子中定义的所有命令。

\def\example#1\par#2\par#3\par#4\par{
\Problem #1 \par             
\ifnum #2 = 1 
\TheSolution #3\\%
\else
\ASolution #3\\%
\ASolution #4\\%
\fi}                   

\example This is a problem with only one solution.\par 1
\par This is the solution of the problem.\par

\example This is another problem with two solutions.\par 2
\par This is a solution of the problem.\par This is the section solution of the problem.\par

在此处输入图片描述

答案3

我创建了以下类,它允许您切换显示解决方案:

% Assignment class for homework and exams.                                                                                                                  
%
% Gabriel Antonius
%
%
\NeedsTeXFormat{LaTeX2e}
\ProvidesClass{myassignment}[2019/02/18 Assigment]
\LoadClass{article}
\RequirePackage{placeins}
\RequirePackage{environ}
\RequirePackage{xifthen}


% \solutiontrue and \solutionfalse control whether solutions are shown
\newif\ifsolution
\solutiontrue
%\solutionfalse


% Redefine these if you want to change the language
\newcommand{\problemlabel}{Problem}
\newcommand{\solutionlabel}{Solution}


% Set up counters for problems and subproblems
\newcounter{ProblemNum}
\newcounter{SubProblemNum}[ProblemNum]
\renewcommand{\theProblemNum}{\arabic{ProblemNum}}
\renewcommand{\theSubProblemNum}{\alph{SubProblemNum}}


% The problem environment is the base unit of content for this class.
\newcommand{\subsectiontitle}{}
\newenvironment{problem}[1]%
  {
  \stepcounter{ProblemNum}
  \renewcommand{\subsectiontitle}{\problemlabel \ \theProblemNum \ifthenelse{\isempty{#1}}{}{\ : #1}}
  \medskip \subsection*{\subsectiontitle}
  \FloatBarrier
  }
  {
  \FloatBarrier
  }


% The subproblem command divides a problem into parts a), b), c), ...
\newcommand*{\subproblem}{%
  \stepcounter{SubProblemNum}%
  {\bf \theSubProblemNum)\hspace{2pt}}
  }


% The solution environment should be used within the problem environment
\NewEnviron{solution}{
  \setcounter{SubProblemNum}{0}
  \ifsolution
    \FloatBarrier
    \subsubsection*{\solutionlabel}
    \BODY
  \fi}

以下是一个示例用法:

\documentclass[12pt]{myassignment}
\usepackage{amsmath}                                                                                                                                        
\setlength{\parindent}{0pt}

\solutiontrue  % <---- Toggle showing solutions or not
%\solutionfalse

\begin{document}

\begin{problem}{Hyperbolic functions}

Let $\cosh(x) = \cos(ix)$ and $\sinh(x) = -i\sin(ix)$.

\subproblem Show that $\cosh^2(x) - \sinh^2(x) = 1$

\subproblem Show that $e^{-x} = \cosh(x) - \sinh(x)$

\begin{solution}

\subproblem
Starting from
\begin{equation}
  1 = \cos^2(x) + \sin^2(x) = \cos^2(x) - \Big( -i\sin(x) \Big)^2
\end{equation}
and using $x=iy$, we get
\begin{equation}
  1 = \cosh^2(y) - \sinh^2(y)
\end{equation}

\subproblem
Starting from
\begin{equation}
  e^{ix} = \cos(x) + i \sin(x)
\end{equation}
and using $x=iy$, we get
\begin{equation}
  e^{-y} = \cosh(y) - \sinh(y)
\end{equation}

\end{solution}
\end{problem}

\end{document}

输出

答案4

我使用以下环境来格式化我的练习表:

\newcounter{aufgabeCounter}
\newenvironment{aufgabe}[1] {%
  \vspace{0.5cm}
  \refstepcounter{aufgabeCounter}\label{#1}
  \noindent \textbf{Aufgabe \theaufgabeCounter.}~% 
} {%
  \vspace{0.5cm}
}

\newenvironment{loesung}[1] {%
  \vspace{0.5cm}
  \noindent \textbf{L\"osung zur Aufgabe~\ref{#1}.}\\% 
} {%
  \vspace{0.5cm}
}

您可以在文本中使用它们,如下所示:

\begin{aufgabe}{aufg:myProblem}
Your problem statement.
\end{aufgabe}

解决方案:

\begin{loesung}{aufg:myProblem}
The solution to your problem.
\end{loesung}

相关内容