# understanding makeatletter with minted / minipage / colorboxes

I found this cool posting for allowing you to include a minted or lstlisting environment inside of a call to a macro:

https://github.com/gpoore/minted/issues/54#issuecomment-58233667

The code

\makeatletter
\newcommand{\foo}{%
\begingroup
\let\do\@makeother\dospecials
\catcode\{=1
\catcode\}=2
\endlinechar\^^J
\foo@i}
\newcommand{\foo@i}[1]{%
\endgroup\scantokens{#1\noexpand}%
\expandafter\foo@cleanup}
\def\foo@cleanup#1^^J{#1 }
%http://tex.stackexchange.com/questions/71049/allow-macro-to-end-without-new-line
\makeatother


I understand that I can use saveboxes, but I do not want to. Substitution \foo for \question, I am trying to implement the equivalent of

\newcommand{\question}[1]{
\noindent\fcolorbox{black}{gray!11}{
\begin{minipage}[t]{0.97\textwidth}
\vspace*{1ex}
#1
\vspace*{1ex}
\end{minipage}
}
}


The problem is after hours of digging around the web, I can't get a clear picture on how this would work. I've tried to make new environments, play with tcolorbox, a bunch of different things. I thought I was making progress when I used \minipage{...} in the first part before \foo@i, and then \endminipage before the \endgroup, and quickly realized that this was not doing what I thought it was. The same goes for \tcolorbox and \endtcolorbox in the same locations.

So basically, is it possible to begin something like a minipage or (t/f)colorbox and wrap it around this? I cannot put something like \tcolorbox[colors etc]{\foo@i}} as the last line (right after \endlinechar), or really any other variant. At this point, endlessly permuting where these commands go is fruitless and I feel I need to be doing something differently.

Thank you for any pointers, e.g. a hacky way to get { and } in. I tried to use the catcodes but I think I'm missing the bigger picture.

The idea would be to use the macro in a situation like this:

\question{
\paragraph*{5: A question from my prof}
Consider some stuff about things i want you to solve:
\begin{minted}{matlab}
for i = 1:n
x(i) = x(i) + 1;
end
\end{minted}
Show that this is a thing that can be done yay.
}


This should do what you want. It takes the linked example, and adds in your \fcolorbox, minipage, etc.

The basic idea is that the macro argument needs to be captured in such a way that an absolute minimum of characters have special meanings (catcodes) so that almost all characters can appear literally, but also so that line endings are preserved (\endlinechar). Then, after the argument is captured, the next macro uses \scantokens to retokenize everything (including preserved line endings) so that it behaves normally where it is actually used.

\makeatletter
\newcommand{\question}{%
\begingroup
\let\do\@makeother\dospecials
\catcode\{=1
\catcode\}=2
\endlinechar\^^J
\question@i}
\newcommand{\question@i}[1]{%
\endgroup
\noindent\fcolorbox{black}{gray!11}{%
\begin{minipage}[t]{0.97\textwidth}%
\vspace*{1ex}%
\scantokens{#1\noexpand}\par%
\vspace*{1ex}%
\end{minipage}%
}%
\expandafter\question@cleanup}
\def\question@cleanup#1^^J{#1 }
\makeatother

• Works like a charm!!! Thank you very much for explaining how this is working, I was misunderstanding how it was expanding everything out but this makes a lot more sense :) It's like I'm relearning tex all over again. Edit: lol i just realized it was your post to start out with, thanks again! – svenevs Sep 8 '16 at 17:32