# How to get minipage to fill remainder of width up to right margin?

I'm trying to design a custom question\solution environment for homework assignments (I'm aware of the exam class but I am choosing to try and make my own) and am having problems with the alignment.

I've set my minipage to use \linewidth for its width, but that extends beyond the right margin, as is shown in my MWE. Is there any way I can make it so that it fills to the right margin without having to set specific lengths? I may change my document margins at a later time and I don't want to have to also change the width of the minipage.

Also, as can be seen, problems are indented quite far over from the left margin, and I'd prefer them to be a tad closer. How can I go about achieving this?

\documentclass{article}

\usepackage{lipsum}
\usepackage{environ}
\usepackage{amsthm}
\usepackage{amssymb}
\usepackage{thmtools}
\usepackage{array}
\usepackage{fancyhdr}
\RequirePackage[left=1in,right=1in,top=1.0in,bottom=1.0in]{geometry}

\makeatletter
\newcommand{\@coursenumber}{{\em No \textbackslash coursenumber specified}}
\newcommand{\@semester}{{\em No \textbackslash semester specified}}
\newcommand{\@studentname}{{\em \textbackslash studentname unknown}}
\newcommand{\@studentid}{{\em \textbackslash studentid unknown}}
\newcommand{\@school}{{\em \textbackslash school unknown}}
\newcommand{\@department}{{\em \textbackslash department unknown}}
\newcommand{\@lecture}{\textbackslash lecture}

\newcommand{\solutionstext}{\bfseries (Solutions)}

% These commands follow the titling package format for titles
% They define user commands to format the subtitle
\newcommand\presubtitle[1]{\gdef\@presubtitle{#1}}
\newcommand\postsubtitle[1]{\gdef\@postsubtitle{#1}}
% This command takes the subtitle as its argument, and uses the titling command
% \maketitlehookb plus the previously defined formatting commands to insert
% the subtitle into the titlepage. It also generates \thesubtitle for subsequent use
\newcommand\subtitle[1]{%
\renewcommand{\maketitlehookb}{\@presubtitle#1\@postsubtitle}
\newcommand\thesubtitle{#1}}
% Now we define the formatting for the subtitle
\presubtitle{\begin{center}\Large} % change this as needed
\postsubtitle{\end{center}}

% These commands set up the headers. They are set up for even and odd pages the same
% Check the fancyhdr documentation for information on how to set them differently
% for odd and even pages
\lfoot{}
\cfoot{\thepage}
\rfoot{}
% Set the width of the header rule. Make 0pt to remove the rule.
\renewcommand{\footrulewidth}{0pt}
% Make the head height match the size of the header
\pagestyle{fancy}
\makeatother

\newcounter{problem}
\setcounter{problem}{0}
\newcounter{problempart}[problem]

\NewEnviron{problem}[1]{
\stepcounter{problem}
\begin{tabular}{m{3mm}l}
\arabic{problem}. & \textbf{#1}\\[1mm]
& \hfill
\begin{minipage}{\linewidth}
\BODY
\end{minipage}
\end{tabular}}

\let\proof\relax

\declaretheoremstyle[
within=section,
spaceabove=5mm,
spacebelow=3mm,
notefont=\normalfont,
bodyfont=\itshape,
notebraces={}{},
]{proof}

\declaretheorem[style=proof,qed=\qedsymbol,name=Proof]{proof}
\declaretheorem[style=proof,qed=\qedsymbol,name=Solution]{solution}

\begin{document}
\begin{problem}{Problem Title}
\lipsum[1]
\begin{solution}
\lipsum[2]

\end{solution}
\end{problem}

\begin{problem}{Problem 2}
\lipsum[3]
\end{problem}
\end{document}

-
From the looks of it, you are creating a tabular with a first column that is 3mm wide and a second with your minipage that is \linewidth wide. It probably runs in the margin by 3mm + the inter-column spacing. Set the inter-column spacing to what you want (e.g. @{\hspace*{1mm}} between the column specifications of \begin{tabular}) and calculate the right width of the second column by removing those lengths from \linewidth. –  Xavier Feb 3 '13 at 5:16

Use

\NewEnviron{problem}[1]{%
\stepcounter{problem}\noindent
\begin{tabular}{m{3mm}p{\dimexpr\linewidth-3mm-3\tabcolsep\relax}@{}}
\arabic{problem}. & \textbf{#1}\\[1mm]
&       \BODY
\end{tabular}}%


When you reach the double digit problems, you have to change the length 3mm to 5mm though. In that case, you may be interested in something like:

\newlength{\mylen}
\settowidth{\mylen}{100}

\NewEnviron{problem}[1]{%
\stepcounter{problem}\noindent
\begin{tabular}{m{\mylen}p{\dimexpr\linewidth-\mylen-3\tabcolsep\relax}@{}}
\arabic{problem}. & \textbf{#1}\\[1mm]
&       \BODY
\end{tabular}}%

-