1

I'd like to parse an optional variable from a custom \newenvironment{example} to an mdframed environment \mytextbox nested in the example environment.

What I'd like to achieve is something like

Example 1.1 [Optional example title]

and the rest of the text in the framed box.

So far, I have the desired mdframed formatting I'd like, (spacing, "Example X.X", etc.) but I cant figure out how to put in the optional title correctly. It's being printed into the mdframed test area instead of the title area.

This is possibly a simple oversight and I'm being stupid with the limited knowledge I have on macros and environments, it's my first time using them, which is why I'm here :)

Below is a MWE and a sample output.

\documentclass[12pt,oneside]{report} \usepackage[a4paper,width=170mm,top=25mm,bottom=25mm]{geometry} \usepackage{lipsum} \usepackage{tikz} \usepackage{mdframed} \newcounter{exampleCounter}[section] \newmdenv[ linewidth=1pt,% frametitlerule=true,% backgroundcolor=black!3,% innerleftmargin=10mm,% innerrightmargin=10mm,% innerbottommargin=10mm,% ] {mytextbox} \newenvironment{example} [1][] {\refstepcounter{exampleCounter} \begin{mytextbox}[frametitle={Example~\thechapter.\theexampleCounter.~##1}] } {\end{mytextbox}} \usepackage{setspace} \setlength{\parindent}{0mm} \setlength{\parskip}{1em} \begin{document} \chapter{First Chapter} \lipsum[1] \begin{example}{\textit{Optional example title}} \lipsum[2] \end{example} \lipsum[3] \end{document} 

enter image description here

1 Answer 1

1

I changed ##1 to #1. Also, in the invocation, one needs to use square brackets for optional arguments: \begin{example}[\textit{Optional example title}].

\documentclass[12pt,oneside]{report} \usepackage[a4paper,width=170mm,top=25mm,bottom=25mm]{geometry} \usepackage{lipsum} \usepackage{tikz} \usepackage{mdframed} \newcounter{exampleCounter}[section] \newmdenv[ linewidth=1pt,% frametitlerule=true,% backgroundcolor=black!3,% innerleftmargin=10mm,% innerrightmargin=10mm,% innerbottommargin=10mm,% ] {mytextbox} \newenvironment{example}[1][] {\refstepcounter{exampleCounter} \begin{mytextbox}[frametitle={Example~\thechapter.\theexampleCounter.~#1}] } {\end{mytextbox}} \usepackage{setspace} \setlength{\parindent}{0mm} \setlength{\parskip}{1em} \begin{document} \chapter{First Chapter} \lipsum[1] \begin{example}[\textit{Optional example title}] \lipsum[2] \end{example} \lipsum[3] \end{document} 

enter image description here

2
  • Yeah, as I suspected... I was dumb. But I discovered a lot while trying to search for alternative solutions. I would highly recommend the exsheets package for question-solution type layouts for tutorials, exercises and exams. I ended up embedding a custom NewQuSolPair from exsheets with a NewDocumentEnvironment offered by the xpatch package. This worked exceptionally well, though the only drawback is I don't have a line under the "Example" heading. But I can live with that. Thanks for the reply though, I really appreciate it. Commented May 18, 2021 at 19:26
  • 1
    @JeanJordaan I'm glad you found an appropriate approach and learned new things along the way. Carry on bravely! Commented May 18, 2021 at 19:34

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.