Inspired by this answer, I would like to have a customized solution environment which accepts an optional argument. This argument can be either numeric or alphabetic, and its value corresponds to the question level whose width is desired to be set to the solution environment.
For example, suppose the question levels have some indices to be: 1 for questions, 2 for parts, 3 for subparts, and 4 for subsubparts. Then the new solution environment might be something like \begin{solution}[2] if I want the solution to span the whole line width of the part environment regardless of where I print this solution.
Finally, if I don't type the optional argument, I would like the width to be set by the parent question level.
\documentclass[answers]{exam}
\usepackage{lipsum}
\usepackage{mdframed}
\renewenvironment{solution}
{\begingroup\par\parshape0%
\begin{mdframed}[skipabove=\baselineskip,
innertopmargin=\baselineskip,
innerbottommargin=\baselineskip,
userdefinedwidth=\textwidth]
\textbf{Solution:}\enspace\ignorespaces}
{\end{mdframed}\par\endgroup}
\begin{document}
\begin{questions}
\question A question.
\begin{parts}
\part part
\begin{solution}
The long solution continues onto another page: \lipsum[1-8]
\end{solution}
\end{parts}
\end{questions}
\lipsum[1]
\end{document}



\begin{solution}[0]) results in the width of\textwidthspanning all the space between the left and right margins as done by the MWE in my question? – Diaa May 07 '19 at 00:13solutionenvironment; also, the optional arguments tomdframedshould be parametrizable). – frougon May 07 '19 at 08:15widesolutionas suggested by this answer. – Diaa May 07 '19 at 08:34wsolution, where thewsuggests this has something to do with width without being too precise. 4) It respects theanswersoption of theexamclass (i.e., the\ifprintanswersconditional). 5) Pre-text and post-text inside themdframedenvironment are customizable. – frougon May 11 '19 at 08:48examclass (ditto for your other question, by the way). – frougon May 11 '19 at 17:02\begin{solution}[1] \centering \repeatit[3]{\myrepeat{130}{.}\\} \vspace*{\baselineskip} \end{solution}doesn't have an escape vspace (i.e.\vspace*{\baselineskip}) as shown here knowing that I loaded the package withreplace-solution-envoption. – Diaa Aug 31 '19 at 23:09{\centering ... \par}\vspace*{whatever}, possibly followed by\nullin case this doesn't work. I'd also suggest doing the dots with\dotfilland replacing the custom loop macros with something standard like\pgffor. No need for\centering, BTW. The following is much simpler and looks good to me:\begin{wsolution}[1] \foreach \i in {1,...,3} {\noindent\dotfill\par} \end{wsolution}→ result.\foreachis defined in thepgfforpackage. – frougon Sep 01 '19 at 12:51exampoint of view), but here, your screenshot shows answer and there is “Answer:” in the box. This has influence on where and how the dots can/should be placed. In my screenshot, the dots starts on the same line as “Solution”. I like it this way, but this is not the case in the aforementioned link. – frougon Sep 01 '19 at 18:26\leftskipand\rightskipcould help for the margins. – frougon Sep 01 '19 at 18:29\vspace*, but I got it by\par\null\par(is it because you wrote in the package\tl_set:Nn \l_examwsolns_mdframed_post_text_tl { \unskip }?). For the titleAnswer, I tweaked your package file by adding\underline{\textbf { Answer \tl_to_str:n { : } }} \par\null\par. – Diaa Sep 01 '19 at 21:10\unskipis a likely culprit for eating your vertical skip (it doesn't distinguish between vertical and horizontal skips). You can get away with\nullor\kern 0pt, but the normal way when you don't want the\unskipwould be to do\examwsolnsSetMdFramedPostText{}before thewsolutionenv that needs the change. Use\begingroupor{before\examwsolnsSetMdFramedPostText{}, and\endgroupor}after thewsolutionenv(s) in order to limit the scope of the change. You don't need to modify my.styto use the word “Answer”, ... – frougon Sep 02 '19 at 06:17\examwsolnsSetMdFramedPreText{\textbf{Answer:}\enspace \ignorespaces}, with grouping as explained before if you want to limit the scope. Underlining is ugly. To control the dots, you can usezrefs\zdotfill(probably the easiest way) or directly one of TeX's\leaders,\cleadersand\xleaderscommands. For the latter, you can look at LaTeX's definition of\dotfill:\leavevmode \cleaders \hb@xt@ .44em{\hss .\hss }\hfill \kern \z@and write your own command with similar code but different values (don't forget\makeatletter...\makeatother). – frougon Sep 02 '19 at 06:27\vspace*problem you encountered: in order to counteract the\unskip, another easy way should be to use (for instance)\par\kern 1cminstead of the\vspace*. This should work because a\kernis not a skip. (Also, contrary to skips, kerns don't get discarded at beginning and end of {lines [for horizontal kerns]} {pages [for vertical kerns]}, but this should matter here unlessmdframedis considering breaking the box across two pages). – frougon Sep 02 '19 at 06:30\examwsolnsSetup{mdframed-pre-text={\textbf{Answer:}\enspace \ignorespaces}, mdframed-post-text={}}(probably I should have done that from the beginning, as this would have removed the need for\examwsolnsSetMdFramedPreTextand\examwsolnsSetMdFramedPostText) . – frougon Sep 02 '19 at 06:41\usepackage[default-width = outer]{examwsolns}? – Diaa Jul 29 '20 at 17:52examwsolnsversion 0.4, you can use the newdefault-levelpackage option:default-level=current|outer|question|part|subpart|subsubpart(the initial value iscurrent, which corresponds to the usual behavior). – frougon Jul 31 '20 at 13:20