9

I want to create a Latex document like the following one:

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.

What do you suggest?

PS: I'm not very familier with TeX environment.

2

4 Answers 4

9

I produced a format, as given in your question. Obviously, one can gussie-up the definitions to achieve a more aesthetically pleasing format. EDITED to give usage syntax desired by questioner.

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

enter image description here

6
  • I deleted following comment by mistake: Is it possible: instead of \ASolution { bla bla }, "\ASolution bla bla" or is it possible only one command for solution not two commands like you did as Asolution and theSolution
    – user706071
    Commented Aug 15, 2013 at 13:15
  • @user706071 I made it so that you can eliminate the braces. However, I cannot collapse \TheSolution and \ASolution into a single macro, because, in essence, they would need to look ahead to see how many solutions would eventually present. But by that time, they are already typeset. Commented Aug 15, 2013 at 13:16
  • Where is the command \theproblem defined?? Commented Aug 30, 2019 at 3:00
  • 1
    @AkivaWeinberger In LaTeX, if a counter is defined, such as problem, the macro \theproblem is also defined to print out the current value of problem. By default, it prints it in arabic, but it can be redefined to print it out in alphabetic or roman numerals, etc. Commented Sep 4, 2019 at 3:47
  • How can we reset the question number when in a new part or chapter?
    – Mercurial
    Commented Sep 4, 2020 at 17:37
1

Based on Steven's solution, I have this idea by defining a new command, delimited by \par, wrapping up all commands previously defined in Steven's post.

\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

enter image description here

1

I created the following class, which allows you to toggle showing the solutions:

% 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}

Here is an example usage:

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

Output

0

I am using the following environments to format my exercise sheets:

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

You can use them in the text as the following:

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

And the solution:

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

You must log in to answer this question.

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