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.

user706071
  • 503
  • 1
  • 4
  • 9

4 Answers4

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

  • 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 Aug 15 '13 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. – Steven B. Segletes Aug 15 '13 at 13:16
  • Where is the command \theproblem defined?? – Akiva Weinberger Aug 30 '19 at 03: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. – Steven B. Segletes Sep 04 '19 at 03:47
  • How can we reset the question number when in a new part or chapter? – Mercurial Sep 04 '20 at 17:37
  • @Mercurial \setcounter{problem}{0}, if I understand your question properly. – Steven B. Segletes Sep 04 '20 at 17:43
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

dustin
  • 18,617
  • 23
  • 99
  • 204
Jesse
  • 29,686
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

Antonius
  • 230
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}