# Answers package: Writing the solution right after the exercise environment (not inside it)

I know the correct format to use the solution in answers package is the following:

\begin{ex}
First exercise
\begin{sol}
First solution.
\end{sol}

\end{ex}


But when the statement of the exercise and its solution are very long the following format has advantage:

\begin{ex}
First exercise
\end{ex}

\begin{sol}
First solution.
\end{sol}


But the problem is that with this format the numbering of the exercises becomes incorrect.

My question is that is there any way that this form works with answers package (probably by changing some settings in answers package)?

\documentclass{article}
\renewcommand{\solutionstyle}[1]{\bfseries Answer to Exercise #1 }
\Newassociation{sol}{Solution}{ans}
\newtheorem{ex}{Exercise}

\begin{document}
\Opensolutionfile{ans}[ans1]

\section{One }

\section{two}
\begin{ex}
First exercise
\end{ex}

\begin{sol}
First solution.
\end{sol}

\Closesolutionfile{ans}
\section{Solutions}
\input{ans1}
\end{document}

• try add \renewcommand{\Currentlabel}{\theex} – touhami Mar 23 '17 at 17:58
• @touhami It seems that your suggestion solves my problem. Please consider to convert your comment to an answer if you like. – Name Mar 23 '17 at 19:02

Here is a solution \renewcommand{\Currentlabel}{\theex}

MWE

\documentclass{article}
\renewcommand{\solutionstyle}[1]{\bfseries Answer to Exercise #1 }
\renewcommand{\Currentlabel}{\theex}
\Newassociation{sol}{Solution}{ans}
\newtheorem{ex}{Exercise}

\begin{document}
\Opensolutionfile{ans}[ans1]

\section{One }

\section{two}
\begin{ex}
First exercise
\end{ex}

\begin{sol}
First solution.
\end{sol}

\Closesolutionfile{ans}
\section{Solutions}
\input{ans1}
\end{document}