Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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)}
\newcommand{\coursenumber}[1]{\renewcommand{\@coursenumber}{#1}}
\newcommand{\semester}[1]{\renewcommand{\@semester}{#1}}
\newcommand{\studentname}[1]{\renewcommand{\@studentname}{#1}}
\newcommand{\studentid}[1]{\renewcommand{\@studentid}{#1}}
\newcommand{\school}[1]{\renewcommand{\@school}{#1}}
\newcommand{\department}[1]{\renewcommand{\@department}{#1}}
\newcommand{\lecture}[1]{\renewcommand{\@lecture}{#1}}

% 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
\lhead{\begin{tabular}{@{}l}\@coursenumber\ (\@semester)\\\@studentname\ (\@studentid)\end{tabular}}
\chead{}
\rhead{\begin{tabular}{r@{}}Assignment \#\@lecture\\\today\end{tabular}}
\lfoot{}
\cfoot{\thepage}
\rfoot{}
% Set the width of the header rule. Make 0pt to remove the rule.
\renewcommand{\headrulewidth}{.5pt}
\renewcommand{\footrulewidth}{0pt}
% Make the head height match the size of the header
\setlength{\headheight}{24pt}
\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,
    headfont=\bfseries,
    notefont=\normalfont,
    bodyfont=\itshape,
    headpunct=\newline,
    %postheadspace={10pt},
    notebraces={}{},
    headformat=\NAME:
]{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}
share|improve this question
    
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
add comment

1 Answer

up vote 1 down vote accepted

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}}%
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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