3

I have a function being used in my document:

\documentclass{article}
% Change "article" to "report" to get rid of page number on title page
\usepackage{amsmath,amsfonts,amsthm,amssymb}
\usepackage{setspace}
\usepackage{Tabbing}
\usepackage{listings}
\usepackage{fancyhdr}
\usepackage{lastpage}
\usepackage{caption}
\usepackage{epstopdf}
\usepackage{Pgfplots}
\usepackage[]{mcode}
\usepackage{tikz}
\usetikzlibrary{shapes,arrows}
\usepackage{extramarks}
\usepackage{chngpage}
\usepackage{soul,color}
\usepackage{graphicx,float,wrapfig}

% In case you need to adjust margins:
\topmargin=-0.45in      %
\evensidemargin=0in     %
\oddsidemargin=0in      %
\textwidth=6.5in        %
\textheight=9.0in       %
\headsep=0.25in         %


\newcommand{\sectionAnswer}[1]
  {% We put this space here to make sure we're disconnected from the previous
   % passage

   \noindent\fbox{\begin{minipage}[c]{\columnwidth}#1\end{minipage}}%
   \enterProblemHeader{\homeworkProblemName}\exitProblemHeader{\homeworkProblemName}%
   \marginpar{\fbox{\homeworkSectionName}}%

   % We put the blank space above in order to make sure this
   % \marginpar gets correctly placed.
   }%

I then later use (I just want to add a few lines of MATLAB code, so notice the package mcode is used):

\sectionAnswer{
\begin{lstlisting}
a = 1;
b = 2;
c = a^2 + b^2;
\end{lstlisting}
}

I am aware that this is because listings (including verbatim content) cannot be passed as arguments to other functions/macros... So I am looking for a workaround? Ideally, I don't want to modify the original macro, because it works quite well, really. So, if I do modify it, \fbox would need to be replaced by something with identical properties.


Following advice from Werner, the macro is now:

\newcommand{\sectionAnswer}[1]
  {% We put this space here to make sure we're disconnected from the previous
   % passage

   \noindent\fbox{\begin{minipage}[c]{\dimexpr\linewidth-2\fboxsep-2\fboxrule}#1\end{minipage}}
 % modify page width from \columnwidth to \dimexpr\linewidth-2\fboxsep-2\fboxrule
   \enterProblemHeader{\homeworkProblemName}\exitProblemHeader{\homeworkProblemName}%
   \marginpar{\fbox{\homeworkSectionName}}%

   % We put the blank space above in order to make sure this
   % \marginpar gets correctly placed.
   }%

I should also note, that this is a modified template, based off Ted Pavlic's.

1

If you wish to maintain the current setup, then you can store the lstlisting inside a box before passing it to \sectionAnswer:

\newsavebox{\codebox}% For storing code in a box

\begin{lrbox}{\codebox}
\begin{lstlisting}
a = 1;
b = 2;
c = a^2 + b^2;
\end{lstlisting}
\end{lrbox}

\sectionAnswer{%
\usebox{\codebox}%
}

Note that your minipage width is probably better set as \dimexpr\linewidth-2\fboxsep-2\fboxrule.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.