# Accessing #1 in the end part of a new environment declaration [duplicate]

I'm working to a document in memoir and I'm struggling trying to define a new environment which would allow me to place a figure in the main text and its caption in the outer margin. This is an extract of my preamble without the definition of such environment.

\documentclass[10pt]{memoir}                        %document class
\setstocksize{240mm}{150mm}

\usepackage[T1]{fontenc}                            %font output
\usepackage[utf8]{inputenc}                         %font input
\usepackage[english]{babel}

\usepackage{lipsum}
\usepackage{marginfix}
\usepackage{graphicx}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% PAGE LAYOUT
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\usepackage{geometry}

\usepackage{fancyhdr}

\settrimmedsize{240mm}{150mm}{*}
\setlength{\trimtop}{0pt}
\setlength{\trimedge}{\stockwidth}
\settypeblocksize{193mm}{84mm}{*}
\setulmargins{27mm}{*}{*}
\setlrmargins{18mm}{*}{*}
\setmarginnotes{3mm}{3.5cm}{\onelineskip}
\checkandfixthelayout

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% CAPTION LAYOUT
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\usepackage{caption}                                %customized captions
\captionsetup{font=footnotesize, labelfont={bf}}
\usepackage{captdef}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% FULLWIDTH
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\usepackage[outermargin=-3.5cm]{fullwidth}

\newenvironment{fullwidthblocked}{%
\blockmargin%
\begin{fullwidth}%
}{%
\end{fullwidth}%
\unblockmargin%
}


I thought I could use fullwidthblocked to define the new environment, so I tried this solution

\newenvironment{figureDX}[1]{%
\begin{fullwidthblocked}%
\begin{minipage}{84mm}%
\centering%
#1%
\end{minipage}%
\hspace{1.5mm}%
\begin{minipage}{3.5cm}%
}{%
\end{minipage}%
\end{fullwidthblocked}%
}


which can be used in the main text writing

\begin{figureDX}{\includegraphics[scale=•]{•}}
\figcaption{•}\label{•}
\end{figureDX}


This actually works, but it is good only for odd pages, since it puts the caption to the right of the image. I then tried to adapt this few lines to introduce their even pages counterpart:

\newenvironment{figureSX}[1]{%
\begin{fullwidthblocked}%
\begin{minipage}{3.5cm}%
}{%
\end{minipage}%
\hspace{1.5mm}%
\begin{minipage}{84mm}%
#1%
\end{minipage}%
\end{fullwidthblocked}%
}


but this returns the error ! Illegal parameter number in definition of \endfigureSX. which I don't know how to solve.

I'm aware the solution I'm proposing is not elegant nor compact, but at least I'd like it to work.. Any ideas how could I fix this?

• \newenvironment does not allow you to access #1 in the end part of the env. You can try using xparse its enviroment creating tools can Dec 17, 2018 at 10:59
• I have just defined the new environment using xparse as you suggested, and it works without problems with the rest of my preamble. Thank you so much! Dec 17, 2018 at 11:40
• BTW: for future visitors, it might be an idea to make the title of the question reflect the actual problem. Which is not making a margin figure, but rather accessing #1 in the end part of a new environment declaration Dec 17, 2018 at 12:10

In normal LaTeX2e the \newenvironment construction does not allow access to #1 in the end part of the environment.
LaTeX3, in this case, xparse to the rescue. It provides
\NewDocumentEnvironment{envname}{ arg specification }{ start }{ end }

with the addition that #1 is accessible from the end part. Note that arg specification is very different from the number based one used by \newenvironment.