[Tex/LaTex] margin differs from \usepackage{geometry}

margins

Hi I have the following preamble in my thesis :

\documentclass[12pt,a4paper,twoside]{report}
\setlength{\textwidth}{17cm}
\setlength{\parskip}{3mm}
\setlength{\parindent}{0mm}

\usepackage[utf8]{inputenc}
\usepackage[top=2cm, bottom=2cm, left=2cm, right=2cm]{geometry}
\usepackage{graphicx}
\graphicspath{{images/}}
\usepackage{setspace}
\usepackage{epsfig}
\usepackage{filecontents}
\usepackage{xparse,nameref}
\usepackage{mathrsfs}
\usepackage[nokeyprefix]{refstyle}
\usepackage{mathtools}
\usepackage{thmtools}
\usepackage{enumerate}
\usepackage{enumitem}
\usepackage{thm-restate}
\usepackage{cleveref}
\usepackage{amsmath}
\usepackage{url}
\usepackage{amsthm}
\DeclareMathOperator{\lcm}{lcm}
\DeclareMathOperator{\ord}{ord}
\usepackage{cellspace}
\setlength\cellspacetoplimit{4pt}
\setlength\cellspacebottomlimit{4pt}
\usepackage{booktabs}
\setlength\defaultaddspace{2ex}
\usepackage{graphicx}
\usepackage{array}
\usepackage{tabularx,ragged2e,booktabs,caption}
\usepackage[figuresleft]{rotating}
\usepackage{rotating}
\newcolumntype{C}{>{\Centering\arraybackslash}X}
\newcommand\swb{{\scriptstyle\Box}} % "small white box"
\usepackage{mathtools}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usetikzlibrary{arrows}
\setcounter{secnumdepth}{3}
\usetikzlibrary{shapes.multipart}
\usepackage{tabu}
\usepackage{amssymb,bm}
\usepackage{framed}
\usepackage{upgreek} %use uptau greek letter
\usepackage{comment} %choose parts not to compile
\usepackage{tikz}
\tikzset{mynode/.style={fill=white,text=black,font=\tiny,inner sep=1pt}}
\usepackage[toc,page]{appendix}
\addtolength{\topmargin}{0.5in}

\newenvironment{numberlists}[1][3\parindent] %for long numberlist command
 {\begin{list}{}{%
   \leftmargin=#1\relax
   \rightmargin=\leftmargin
   \itemsep=\jot
   \parsep=0pt
   \partopsep=0pt
   \labelsep=0pt}}
 {\end{list}}
\newcommand\numlist[2]{%
  \item[]\makebox[0pt][r]{$#1=\lbrack$}%
  \begingroup
  \begingroup\lccode`~=`,\lowercase{\endgroup\def~}{\mathcomma\penalty0 }%
  \mathcode`,="8000
  \thinmuskip=6mu plus 6mu minus 2mu
  $#2\rbrack$%
  \endgroup
}
\mathchardef\mathcomma=\mathcode`, %end of numberlist command
\setlength{\oddsidemargin}{-1in}   \setlength{\evensidemargin}{-1in}
\addtolength{\oddsidemargin}{20mm}\addtolength{\evensidemargin}{20mm}
\setlength{\marginparwidth}{40pt} \setlength{\marginparsep}{10pt}
\setlength{\topmargin}{-5mm}      \setlength{\headsep}{0.5in}
\setlength{\textheight}{227mm}    \setlength{\textwidth}{165mm}
\renewcommand\floatpagefraction{0.8} %   Default = 0.5
\usepackage{listings}
\usepackage{color}
\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{gray}{rgb}{0.5,0.5,0.5}
\definecolor{mauve}{rgb}{0.58,0,0.82}
\lstset{
  language=Java,
  aboveskip=3mm,
  belowskip=3mm,
  showstringspaces=false,
  columns=flexible,
  basicstyle={\small\ttfamily},
  numbers=none,
  numberstyle=\tiny\color{gray},
  keywordstyle=\color{blue},
  commentstyle=\color{dkgreen},
  stringstyle=\color{mauve},
  breaklines=true,
  breakatwhitespace=true,
  tabsize=3
}

I know that my premable is a mess and am trying to fix it. The problem here is that i have \usepackage[top=2cm, bottom=2cm, left=2cm, right=2cm]{geometry} and it does not seem to work because when I print my thesis the margin is different. top margin is 4.5 cm, bottom, left and right margin are all 2.5 cm. I think this has something to do with my numberlist command in my preamble (because there's a different margin set up in that command). Is there a way to fix it. The grad school refuse to accept my thesis for submission because of the margin issue. It should be 2cm top bottom left right.

Best Answer

\documentclass[12pt,a4paper,twoside]{report}

Remove this line, it will be undone anyway by the geometry package

\setlength{\textwidth}{17cm}

Unrelated to your margins but are you sure that you want a fixed length for \parskip together with \flushbottom it will mean that pages with a paragraph are almost always going to be infeasible and generate under or over full box warnings.

\setlength{\parskip}{3mm}

\setlength{\parindent}{0mm}

\usepackage[utf8]{inputenc}

This is the form that you want to specify the page size, but normally you should also use heightrounded so that the specified size matches the baselinesip.

\usepackage[top=2cm, bottom=2cm, left=2cm, right=2cm]{geometry}

\usepackage{graphicx}
\graphicspath{{images/}}
\usepackage{setspace}

Don't use epsfig

\usepackage{epsfig}

\usepackage{filecontents}
\usepackage{xparse,nameref}
\usepackage{mathrsfs}
\usepackage[nokeyprefix]{refstyle}
\usepackage{mathtools}
\usepackage{thmtools}

don't use enumerate if you are using enumitem (they patch the same environment)

\usepackage{enumerate}

\usepackage{enumitem}

\usepackage{thm-restate}
\usepackage{cleveref}
\usepackage{amsmath}
\usepackage{url}
\usepackage{amsthm}
\DeclareMathOperator{\lcm}{lcm}
\DeclareMathOperator{\ord}{ord}
\usepackage{cellspace}
\setlength\cellspacetoplimit{4pt}
\setlength\cellspacebottomlimit{4pt}
\usepackage{booktabs}
\setlength\defaultaddspace{2ex}
\usepackage{graphicx}
\usepackage{array}
\usepackage{tabularx,ragged2e,booktabs,caption}

\usepackage[figuresleft]{rotating}

Don't load rotating here if you loaded it on the previous line

\usepackage{rotating}

\newcolumntype{C}{>{\Centering\arraybackslash}X}
\newcommand\swb{{\scriptstyle\Box}} % "small white box"
\usepackage{mathtools}
\usepackage{tikz}
\usetikzlibrary{positioning}
\usetikzlibrary{arrows}
\setcounter{secnumdepth}{3}
\usetikzlibrary{shapes.multipart}
\usepackage{tabu}
\usepackage{amssymb,bm}
\usepackage{framed}
\usepackage{upgreek} %use uptau greek letter
\usepackage{comment} %choose parts not to compile

You have loaded tikz already

\usepackage{tikz}

\tikzset{mynode/.style={fill=white,text=black,font=\tiny,inner sep=1pt}}
\usepackage[toc,page]{appendix}

Don't do this it undoes the geometry settings

\addtolength{\topmargin}{0.5in}

\newenvironment{numberlists}[1][3\parindent] %for long numberlist command
 {\begin{list}{}{%
   \leftmargin=#1\relax
   \rightmargin=\leftmargin
   \itemsep=\jot
   \parsep=0pt
   \partopsep=0pt
   \labelsep=0pt}}
 {\end{list}}
\newcommand\numlist[2]{%
  \item[]\makebox[0pt][r]{$#1=\lbrack$}%
  \begingroup
  \begingroup\lccode`~=`,\lowercase{\endgroup\def~}{\mathcomma\penalty0 }%
  \mathcode`,="8000
  \thinmuskip=6mu plus 6mu minus 2mu
  $#2\rbrack$%
  \endgroup
}

The comment here is wrong, numberlist ended above.

\mathchardef\mathcomma=\mathcode`, %end of numberlist command

Delete all these lines they undo the geometry settings

\setlength{\oddsidemargin}{-1in}   \setlength{\evensidemargin}{-1in}
\addtolength{\oddsidemargin}{20mm}\addtolength{\evensidemargin}{20mm}
\setlength{\marginparwidth}{40pt} \setlength{\marginparsep}{10pt}
\setlength{\topmargin}{-5mm}      \setlength{\headsep}{0.5in}
\setlength{\textheight}{227mm}    \setlength{\textwidth}{165mm}

Are you sure you want this, it makes float pages hard to form and increases the chance that all floats go to the end of the document

\renewcommand\floatpagefraction{0.8} %   Default = 0.5

\usepackage{listings}
\usepackage{color}
\definecolor{dkgreen}{rgb}{0,0.6,0}
\definecolor{gray}{rgb}{0.5,0.5,0.5}
\definecolor{mauve}{rgb}{0.58,0,0.82}
\lstset{
  language=Java,
  aboveskip=3mm,
  belowskip=3mm,
  showstringspaces=false,
  columns=flexible,
  basicstyle={\small\ttfamily},
  numbers=none,
  numberstyle=\tiny\color{gray},
  keywordstyle=\color{blue},
  commentstyle=\color{dkgreen},
  stringstyle=\color{mauve},
  breaklines=true,
  breakatwhitespace=true,
  tabsize=3
}