\usepackage{enumerate}
\usepackage{amssymb}
\usepackage{amsbsy}
\usepackage{amsmath}
\usepackage{amsthm}
\usepackage{amsfonts}
\usepackage{latexsym}
\usepackage{graphicx}
\usepackage{xifthen} % Allows us to put in optional arguments to questions
\usepackage{comment}
\usepackage{xcolor}
\usepackage{xstring}
\usepackage{version}
% \usepackage{fullpage}

\def\usebiggeometry{%
  \usepackage[top=2.54cm,left=2.54cm,right=2.54cm,bottom=2.54cm]{geometry}}
\definecolor{darkblue}{rgb}{0,0,.75}
\def\bluehyperref{%
  \usepackage[colorlinks=true,allcolors=darkblue]{hyperref}
}
  
% \setlength{\topmargin}{1cm}
%% \setlength{\textwidth}{15cm}
%% \setlength{\textheight}{8.5in}
%% \setlength{\oddsidemargin}{1cm}

%%% New version of \caption puts things in smaller type, single-spaced 
%%% and indents them to set them off more from the text.
\makeatletter
\long\def\@makecaption#1#2{
  \vskip 0.8ex
  \setbox\@tempboxa\hbox{\small {\bf #1:} #2}
  \parindent 1.5em  %% How can we use the global value of this???
  \dimen0=\hsize
  \advance\dimen0 by -3em
  \ifdim \wd\@tempboxa >\dimen0
  \hbox to \hsize{
    \parindent 0em
    \hfil 
    \parbox{\dimen0}{\def\baselinestretch{0.96}\small
      {\bf #1.} #2
      %%\unhbox\@tempboxa
    } 
    \hfil}
  \else \hbox to \hsize{\hfil \box\@tempboxa \hfil}
  \fi
}
\makeatother

\newcommand{\mc}[1]{\mathcal{#1}}
\def\mbf#1{\mathbf{#1}}
\def\mrm#1{\mathrm{#1}}
\def\mbi#1{\boldsymbol{#1}} % Bold and italic (math bold italic)
\def\v#1{\mbi{#1}} % Vector notation
\newcommand{\norm}[1]{\left\|{#1}\right\|} % A norm with 1 argument
\newcommand{\lone}[1]{\norm{#1}_1} % l1 norm
\newcommand{\ltwo}[1]{\norm{#1}_2} % l2 norm
\newcommand{\linf}[1]{\norm{#1}_\infty} % l-infinity norm
\newcommand{\lzero}[1]{\norm{#1}_0} % l-zero norm
\newcommand{\dnorm}[1]{\norm{#1}_*} % Dual norm
\newcommand{\lfro}[1]{\left\|{#1}\right\|_{\rm Fr}} % Frobenius norm
\newcommand{\matrixnorm}[1]{\left|\!\left|\!\left|{#1}
  \right|\!\right|\!\right|} % Matrix norm with three bars
\newcommand{\matrixnorms}[1]{|\!|\!|{#1}|\!|\!|} % Small matrix norm
\newcommand{\opnorm}[1]{\matrixnorm{#1}_{\rm op}}
\newcommand{\opnorms}[1]{\matrixnorms{#1}_{\rm op}}
\newcommand{\normbigg}[1]{\bigg\|{#1}\bigg\|} % A norm with 1 argument and bigg
                                              % brackets.
\newcommand{\lonebigg}[1]{\normbigg{#1}_1} % l1 norm
\newcommand{\ltwobigg}[1]{\normbigg{#1}_2} % l2 norm
\newcommand{\linfbigg}[1]{\normbigg{#1}_\infty} % l-infinity norm
\newcommand{\norms}[1]{\|{#1}\|} % A norm with 1 argument and normal (small)
                                 % brackets.
\newcommand{\lones}[1]{\norms{#1}_1} % l1 norm with small brackets
\newcommand{\ltwos}[1]{\norms{#1}_2} % l2 norm with small brackets
\newcommand{\linfs}[1]{\norms{#1}_\infty} % l-infinity norm with small brackets

\newcommand{\hinge}[1]{\left({#1}\right)_+}
\newcommand{\softthresh}[1]{\mathsf{S}_\lambda\left({#1}\right)}

\def\defeq{:=}
\newcommand{\what}[1]{\widehat{#1}} % Wide hat 
\newcommand{\wt}[1]{\widetilde{#1}} % Wide tilde
\newcommand{\wb}[1]{\overline{#1}} % Wide bar

\newcommand{\half}{\frac{1}{2}}

\newcommand{\reals}{\mathbb{R}} % Real number symbol
\newcommand{\R}{\mathbb{R}}
\newcommand{\C}{\mathbb{C}} % Complex numbers
\def\integers{\mathbb{Z}} % Integer symbol
\def\Z{\mathbb{Z}}
\def\rationals{\mathbb{Q}} % Rational numbers
\def\Q{\mathbb{Q}}
\def\naturals{\mathbb{N}} % Natural numbers
\def\N{\mathbb{N}}
\newcommand{\sphere}{\mathbb{S}}
\newcommand{\ball}{\mathbb{B}}
\newcommand{\rank}{\mathop{\textup{rank}}}

\newcommand{\ones}{\mathbf{1}}
\newcommand{\zeros}{\mathbf{0}}
\newcommand{\diam}{\mathop{\rm diam}}

\newcommand{\<}{\langle} % Angle brackets
\renewcommand{\>}{\rangle}

\def\iff{\Leftrightarrow}
\def\choose#1#2{\left(\begin{array}{c}{#1} \\ {#2}\end{array}\right)}
\def\chooses#1#2{{}_{#1}C_{#2}}
\def\twovec#1#2{\left[\begin{array}{c}{#1} \\ {#2}\end{array}\right]}
\def\threevec#1#2#3{\left[\begin{array}{c}{#1} \\ {#2} \\ {#3} \end{array}\right]}
\def\nvec#1#2#3{\left[\begin{array}{c}{#1} \\ {#2} \\ \vdots \\ {#3}\end{array}\right]} % An n-vector with three arguments
\def\e{\mrm{E}} % Expectation symbol
\def\E{\mathbb{E}} % Expectation symbol
\def\P{\mathbb{P}} % Probability symbol
\newcommand{\var}{{\rm Var}} % Variance
\newcommand{\cov}{{\rm Cov}} % Covariance
\newcommand{\med}{{\rm Med}} % Median
\newcommand{\simiid}{\stackrel{\rm iid}{\sim}}
\newcommand{\indic}[1]{\mbf{1}\left\{#1\right\}} % Indicator function
\newcommand{\openleft}[2]{\left({#1},{#2}\right]} % Interval open on left
\newcommand{\openright}[2]{\left[{#1},{#2}\right)} % Interval open on right
% Information symbol
\newcommand{\info}[3][{}]{\mathbb{I}_{#1}\left({#2};{#3}\right)}
% Distances between probability measures
\newcommand{\tvnorm}[1]{\norm{#1}_{\rm TV}} % Total variation
\newcommand{\dkl}[2]{D_{\rm kl}\left({#1} |\!| {#2}\right)} % KL divergence
\newcommand{\fdiv}[2]{D_f\left({#1} |\!| {#2}\right)} % f divergence

\providecommand{\psiinfluence}{\psi^{\rm Inf}}

% Convergence of random variables
\def\cd{\stackrel{d}{\rightarrow}}
\def\cas{\stackrel{a.s.}{\rightarrow}}
\def\cp{\stackrel{p}{\rightarrow}}
\newcommand{\eqdist}{\stackrel{d}{=}}

\newcommand{\floor}[1]{\left\lfloor{#1} \right\rfloor}
\newcommand{\ceil}[1]{\left\lceil{#1} \right\rceil}

\providecommand{\argmax}{\mathop{\rm argmax}} % Defining math symbols
\providecommand{\argmin}{\mathop{\rm argmin}}
\providecommand{\dom}{\mathop{\rm dom}}
\providecommand{\diag}{\mathop{\rm diag}}
\providecommand{\tr}{\mathop{\rm tr}}
\providecommand{\abs}{\mathop{\rm abs}}
\providecommand{\card}{\mathop{\rm card}}
\providecommand{\sign}{\mathop{\rm sign}}
\providecommand{\sgn}{\mathop{\rm sgn}}
\providecommand{\cl}{\mathop{\rm cl}}
\providecommand{\interior}{\mathop{\rm int}}
\providecommand{\conv}{\mathop{\rm Conv}}
\providecommand{\relint}{\mathop{\rm relint}}
\providecommand{\vol}{\textup{Vol}}
\providecommand{\supp}{\mathop{\rm supp}}
\providecommand{\linspan}{\mathop{\textup{span}}}

% Statistical things
\newcommand{\normal}{\mathsf{N}}
\newcommand{\poisson}{\mathsf{Poisson}}
\newcommand{\stddev}{\sigma}
\newcommand{\uniform}{\mathsf{Uni}}


\newcommand{\opt}{^\star}
\newcommand{\subopt}{_\star}
\newcommand{\eqnd}{\stackrel{d}{=}} % Equals in distribution
\newcommand{\loss}{\ell}
\newcommand{\fleft}{f_{\rm left}}
\newcommand{\fright}{f_{\rm right}}
\providecommand{\bernoulli}{\mathsf{Bernoulli}}
\newcommand{\score}{\dot{\ell}}
\newcommand{\test}{\psi}

\newcommand{\fisher}[1][]{%
  \ifthenelse{\isempty{#1}}{$
    I
  }{%
    I_{#1}
  }
}

% Optimization stuff

\providecommand{\minimize}{\mathop{\rm minimize}}
\providecommand{\maximize}{\mathop{\rm maximize}}
\providecommand{\subjectto}{\mathop{\rm subject\;to}}
\newcommand{\kl}[2]{D_{\rm kl}\left({#1} |\!| {#2} \right)}
\newcommand{\dhel}{d_{\rm hel}}
\newcommand{\helaff}{A_{\rm hel}}

\def\eqnref#1{(\ref{#1})}
\def\figref#1{Fig. \ref{#1}}

% Proof environments
% The Theorems are numbered consecutively
% Lemmas are numbered by section, and observations, claims, facts, and 
% assumptions take their numbering. Propositions and definitions have their
% own numbering by section.
\newtheorem{theorem}{Theorem}
\newtheorem{lemma}{Lemma}
\newtheorem{corollary}[theorem]{Corollary}
\newtheorem{observation}[theorem]{Observation}
\newtheorem{proposition}[theorem]{Proposition}
\newtheorem{definition}{Definition}[section]
\newtheorem{claim}[theorem]{Claim}
\newtheorem{fact}[theorem]{Fact}
\newtheorem{assumption}{Assumption}
\newtheorem{conjecture}[theorem]{Conjecture}

% Number lemmas/theorems within the questions to which they belong.
\renewcommand{\thelemma}{\arabic{section}.\arabic{question}.\arabic{lemma}}
\renewcommand{\thetheorem}{\arabic{section}.\arabic{question}.\arabic{theorem}}

\renewenvironment{proof}{\noindent{\bf Proof}\hspace*{1em}}{\qed\bigskip\\}
\newenvironment{proof-sketch}{\noindent{\bf Sketch of Proof}
  \hspace*{1em}}{\qed\bigskip\\}
\newenvironment{proof-idea}{\noindent{\bf Proof Idea}
  \hspace*{1em}}{\qed\bigskip\\}
\newenvironment{proof-of-lemma}[1][{}]{\noindent{\bf Proof of Lemma {#1}}
  \hspace*{1em}}{\qed\bigskip\\}
\newenvironment{proof-of-proposition}[1][{}]{\noindent{\bf
    Proof of Proposition {#1}}
  \hspace*{1em}}{\qed\bigskip\\}
\newenvironment{proof-of-theorem}[1][{}]{\noindent{\bf Proof of Theorem {#1}}
  \hspace*{1em}}{\qed\bigskip\\}
\newenvironment{inner-proof}{\noindent{\bf Proof}\hspace{1em}}{
  $\bigtriangledown$\medskip\\}
%% \newenvironment{proof-of-lemma}[1][{}]{\noindent{\bf Proof of Lemma {#1}}
%%   \hspace*{1em}\renewcommand{\qedsymbol}{$\bigtriangledown$}}{\qed\bigskip\\}
\newenvironment{proof-attempt}{\noindent{\bf Proof Attempt}
  \hspace*{1em}}{\qed\bigskip\\}
\newenvironment{proofof}[1]{\noindent{\bf Proof} of {#1}:
  \hspace*{1em}}{\qed\bigskip\\}
\newenvironment{remark}{\noindent{\bf Remark}
  \hspace*{1em}}{\bigskip}


%%%% Question environments %%%%

\newcounter{question}
\numberwithin{question}{section}
\numberwithin{lemma}{question}
\numberwithin{equation}{section}

\newenvironment{question}[1][]{
  \refstepcounter{question}
  \ifthenelse{\isempty{#1}}{%
    \noindent \textbf{Question \thequestion:}\hspace*{.25em}
  }{%
    \noindent \textbf{Question \thequestion} ({#1})\textbf{:}\hspace*{.25em}
  }
}{%
  \par
  \vspace{.25cm}
}

\ifdefined\showanswer
\newenvironment{answer}[1][]{
  \ifthenelse{\isempty{#1}}{%
    \noindent{\bf Answer:}\hspace*{1em}
  }{%
    \noindent \textbf{Answer to {#1}:}\hspace*{1em}
  }
}{%
  \qed\smallskip\\
}
\else
\newenvironment{answer}{}{}
\excludecomment{answer}
\fi

\makeatletter
\def\@maketitle{%
  \newpage
  \null
  \vskip -1em%
  \begin{center}%
    \let \footnote \thanks
    \vskip -1em%
    {\Large \textsc{\@title} \par}%
    \vskip 1em%
    {\large
      \lineskip .5em%
      \begin{tabular}[t]{c}%
        \@author
      \end{tabular}\par}%
    \vskip 1em%
    {\large \@date}%
  \end{center}%
  \par
  \ifdefined\showanswer%
  \begin{center}%
    \fcolorbox{black}{pink}{%
      \begin{minipage}{.8\columnwidth}
        This solution set is copyright John Duchi and Stanford University, 2024.
        You \emph{may not} post it anywhere online and may use it only for
        personal use. Posting it online or hosting it on a website is a violation
        of both copyright law and the Stanford Honor Code.
      \end{minipage}
    }%
  \end{center}%
  %
  \vskip 1.5em}
\makeatother



% Makes equation numbers have (1.1) style
% \numberwithin{equation}{section}
% \numberwithin{equation}{subsection}
