How to get a Solution environment to break across pages in LaTeX - latex

For typesetting math homework I've created (simple) custom environments for problems and solutions.
The code I uses is
\newenvironment{question}{
\textbf{Problem \arabic{question}.}
\begin{center}
\begin{minipage}[t]{.9\textwidth}} {\end{minipage}
\end{center}
\vspace{12pt} \stepcounter{question}}
and
\newlength{\currentparskip}
\newenvironment{solution} {
\setlength{\currentparskip}{\parskip}
\begin{center}
\begin{minipage}[t]{.9\textwidth}
\setlength{\parskip}{\currentparskip}
\textbf{Solution:} \\[12pt] }
{\end{minipage}
\end{center}
\vspace{24pt} }
It works well when the problems and solutions are fairly short, but since I achieve the positioning I want with a minipages it does not break across pages.
To date the only problem I've had is solutions being too long so that I would get the problem on one page with a bunch of white space and then the solution on its own page. This has been annoying, but I've ignored it.
Now I'm having to address the issue because one of my solutions is so long it doesn't fit on its own page,
I (think I) know that my issue is that I'm using minipages, but I'm not sure what else to use in order have different margins for my solutions. What structure should I be using instead?
Minimal working example:
\documentclass[12pt]{article}
\usepackage{parskip}
\usepackage{lipsum}
\newcounter{question}
\setcounter{question}{1}
\newlength{\currentparskip}
\newenvironment{question}{
\setlength{\currentparskip}{\parskip}
\textbf{Problem \arabic{question}.} \begin{center} \begin{minipage}[t]{.9\textwidth}} {\end{minipage} \end{center} \vspace{12pt} \stepcounter{question}}
\newenvironment{solution} {
\setlength{\currentparskip}{\parskip}
\begin{center} \begin{minipage}[t]{.9\textwidth}
\setlength{\parskip}{\currentparskip}
\textbf{Solution:} \\[12pt] } {\end{minipage} \end{center} \vspace{24pt} }
\setlength{\parskip}{24pt}
\begin{document}
\begin{question}
A question with an answer too long to fit on the same page as the question is on.
\lipsum[100]
\end{question}
\begin{solution}
\lipsum[100]
\lipsum[100]
\lipsum[100]
\end{solution}
\begin{question}
A question with too long of a solution even for its own page.
\end{question}
\begin{solution}
\lipsum[100]
\lipsum[100]
\lipsum[100]
\lipsum[100]
\lipsum[100]
\lipsum[100]
\end{solution}
\end{document}

One possibility using the tcolorbox package
\documentclass[12pt]{article}
\usepackage{parskip}
\usepackage{lipsum}
\usepackage[most]{tcolorbox}
\tcbsetforeverylayer{
fonttitle=\bfseries,
left=.1\textwidth,
right=.1\textwidth,
breakable,
enhanced,
interior hidden,
frame hidden,
coltitle=black,
}
\newtcolorbox[auto counter]{question}{
title={Problem~\thetcbcounter.},
attach boxed title to top left={xshift=-3mm},
}
\newtcolorbox{solution}{title={Solution:}}
\begin{document}
A question with an answer too long to fit on the same page as the question is on.
\begin{question}
A question with an answer too long to fit on the same page as the question is on.
\lipsum[100]
\end{question}
\begin{solution}
\lipsum[100]
\lipsum[100]
\lipsum[100]
\end{solution}
\begin{question}
A question with too long of a solution even for its own page.
\end{question}
\begin{solution}
\lipsum[100]
\lipsum[100]
\lipsum[100]
\lipsum[100]
\lipsum[100]
\lipsum[100]
\end{solution}
\end{document}

One approach is to use a custom list; all the altered formatting including margins can span multiple pages. Although LaTeX provides a solution for that, it's better to load enumitem for even more flexibility.
\documentclass[12pt]{article}
\usepackage{parskip}
\usepackage{kantlipsum}
\usepackage{enumitem}
\newcounter{question}\renewcommand{\thequestion}{Problem \arabic{question}.}
\newlength{\currentparskip}
\AtBeginDocument{\setlength\parskip{24pt}}
\newlist{lquestion}{itemize}{1}
\newlist{lsolution}{itemize}{1}
\setlist[lquestion,lsolution]{
label={},
align=left,
wide,
leftmargin=0.05\linewidth,
rightmargin=0.05\linewidth,
}
\setlist*[lsolution]{labelindent=0.05\linewidth}
\newenvironment{question}{%
\stepcounter{question}
\setlength{\currentparskip}{\parskip}
\begin{lquestion}
\item[\textbf{\thequestion}]\vphantom{X}\par\nopagebreak\vspace{-15pt}%
}{\end{lquestion}}
\newenvironment{solution}{%
\begin{lsolution}
\setlength{\currentparskip}{\parskip}
\item[\textbf{Solution:}]\vphantom{X}\par\nopagebreak\vspace{-15pt}%
}{\end{lsolution}}
\begin{document}
\begin{question}
A question with an answer too long to fit on the same page as the question is on.
\kant[1]
\end{question}
\begin{solution}
\kant*[2]
\[
y = f(x)
\]
\kant[3-4]
\end{solution}
\begin{question}
A question with too long of a solution even for its own page.
\end{question}
\begin{solution}
\kant[5-10]
\end{solution}
\end{document}

For now I am going to use the package changepage: https://www.ctan.org/pkg/changepage
It seems to do what I need for now.
The minimal working example becomes:
\documentclass[12pt]{article}
\usepackage{parskip}
\usepackage{lipsum}
\usepackage{changepage}
\newcounter{question}
\setcounter{question}{1}
\newlength{\currentparskip}
\newenvironment{question}{
\setlength{\currentparskip}{\parskip}
\textbf{Problem \arabic{question}.}
\begin{adjustwidth}{.5cm}{0cm}} {\end{adjustwidth} \vspace{12pt} \stepcounter{question}}
\newenvironment{solution} {
\begin{adjustwidth}{.5cm}{0cm}
\textbf{Solution:} \\[12pt] }
{\end{adjustwidth} \vspace{12pt} \stepcounter{question}}
\setlength{\parskip}{12pt}
\begin{document}
\begin{question}
A question with an answer too long to fit on the same page as the question is on.
\lipsum[100]
\end{question}
\begin{solution}
\lipsum[100]
\lipsum[100]
\lipsum[100]
\end{solution}
\begin{question}
A question with too long of a solution even for its own page.
\end{question}
\begin{solution}
\lipsum[100]
\lipsum[100]
\lipsum[100]
\lipsum[100]
\lipsum[100]
\lipsum[100]
\end{solution}
\end{document}
This solution was inspired by this post: https://tex.stackexchange.com/questions/588/how-can-i-change-the-margins-for-only-part-of-the-text.

Related

"landscape" leads to page shrinking in Latex?

If you run this, you can see that all pages after \begin{landscape} are too small."Page 4" at the bottom for example should be 90% into the page, but it's more like 70%. How do I fix this?
\documentclass{exam}
\usepackage{pdflscape}
\usepackage{pdfpages}
\usepackage{lipsum}
\printanswers
\begin{document}
Page1
\newpage
Page2
\begin{landscape}
\begin{solution}
\includegraphics{example-image-a}
\end{solution}
\end{landscape}
\lipsum
\end{document}

includepdf overlays document stack on one page

I'm trying to include a PDF in a document using \includepdf. The problem is that Latex is putting all sites of the pdf on one page of the document (over each other). I'm a bit lost on that one and didn't found any solutions.
\begin{figure}[H]
\includepdf[landscape,pages={1-6}]{Zeichnungen/Motor}
\label{Motor}
\end{figure}
If you want to use \includepdf, don't put it in a figure environment.
If you do want to use the figure environment, use the good old \inlcudegraphics
\documentclass{article}
\usepackage{graphicx}
\usepackage{pdfpages}
\usepackage{pgffor}
\begin{document}
with pdfpages:
\includepdf[pages={1-6}]{example-image-duck}
\clearpage
with good old graphicx
\foreach \x in {1,...,6}{
\begin{figure}[htbp]
\includegraphics[page=\x]{example-image-duck}
\label{Motor-\x}
\end{figure}
}
\end{document}

Page numbers remain in center for every first page in chapter [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 2 years ago.
Improve this question
I have a problem with the page numbers. They should be one the right which they are, except for the very first page in every new chapter. There they're still in the center.
Edit: This is now compilable so you can have a look on the page numbers. I would really appreciate any help.
\documentclass[
11pt,
a4paper,
DIV=13,
BROC=5mm
twoside,
parskip=half,
bibtotoc,
openright,
cleardoubleempty
]{scrreprt}
\usepackage[utf8]{inputenc}
\usepackage{blindtext}
\usepackage{scrlayer-scrpage}
\pagestyle{scrheadings}
\cfoot{}
\chead{}
\automark[section]{chapter}
\ohead{\headmark}
\ofoot{\pagemark}
\begin{document}
\chapter{test}
\blindtext
\newpage
\blindtext
\chapter{failed}
\blindtext
\newpage
\blindtext
\end{document}
I can't get rid of that, any suggestions?
You can clear the numbers in the centre with \clearscrheadfoot and then add them on the outer edge with \ofoot*{\pagemark}
\documentclass[
11pt,
a4paper,
DIV=13,
BROC=5mm
twoside,
parskip=half,
bibtotoc,
openright,
cleardoubleempty
]{scrreprt}
\usepackage[utf8]{inputenc}
\usepackage{blindtext}
\usepackage{scrlayer-scrpage}
\pagestyle{scrheadings}
\cfoot{}
\chead{}
\automark[section]{chapter}
\clearscrheadfoot
\ohead{\headmark}
\ofoot*{\pagemark}
\begin{document}
\chapter{test}
\blindtext
\newpage
\blindtext
\chapter{failed}
\blindtext
\newpage
\blindtext
\end{document}

Changing Margin in LaTeX

I'm new to LaTex and I wanted to know how I can change the Margins of my scrreport so that the chapter title, text and basically everythin starts a bit higher and ends a bit lower. In my opinion there is too much empty space before anything starts.
If I use this simple example:
\documentclass[12pt,a4paper,twoside]{scrreport}
\usepackage{blindtext}
\begin{document}
\chapter{First Chapter}
\blindtext
\end{document}
I think the margins from top and bottom are too big. So i want everything to move up a little bit.
Thanks!
Method 1:
Choose one of the predefined layouts. You'll find a list of available options in the koma script documentation.
\documentclass[12pt,a4paper,twoside,DIV=15]{scrreport}
\usepackage{blindtext}
\begin{document}
\chapter{First Chapter}
\blinddocument
\end{document}
Method 2:
Setting up the text area manually. You should be really sure that you know what you are doing to get an aesthetically pleasant result.
\documentclass[12pt,a4paper,twoside]{scrreport}
\areaset[current]{168.00mm}{250mm}
\usepackage{blindtext}
\begin{document}
\chapter{First Chapter}
\blinddocument
\end{document}

Vertically centering a title page

I'm trying to vertically center a title on a custom-sized page with latex. I've written the following code, but for some reason it doesn't center. Could someone please point me to what's wrong with it?
Thanks!
\documentclass{article}
\setlength{\pdfpagewidth}{88.184mm}
\setlength{\pdfpageheight}{113.854mm}
\usepackage[margin=0.5cm, paperwidth=88.184mm, paperheight=113.854mm]{geometry}
\title{[[title]]}
\date{[[date]]}
\author{[[author]]}
\begin{document}
\vspace{\fill}
\maketitle
\vspace{\fill}
\newpage
[[text]]
\end{document}
There are two small bugs in your code.
First, if you want the \vspace to work at the beginning or end of a page, you should use the starred version (\vspace*).
This would work, but \maketitle is a pretty complicated macro, and if used like in your example, it just puts the title at the second page. You can use the titlepage environment, which gives you much more command over how the title page looks like -- including the spacing. For example, you could use the following code:
\documentclass{article}
\setlength{\pdfpagewidth}{88.184mm}
\setlength{\pdfpageheight}{113.854mm}
\usepackage[margin=0.5cm, paperwidth=88.184mm, paperheight=113.854mm]{geometry}
\begin{document}
\begin{titlepage}
\vspace*{\fill}
\begin{center}
{Huge [[title]]}\\[0.5cm]
{Large [[author}\\[0.4cm]
[[date]]
\end{center}
\vspace*{\fill}
\end{titlepage}
[[text]]
\end{document}
\null % Empty line
\nointerlineskip % No skip for prev line
\vfill
\let\snewpage \newpage
\let\newpage \relax
\maketitle
\let \newpage \snewpage
\vfill
\break % page break
If you want to make everything work even with \maketitle put your \vspace*{\fill} inside the first and the last attribute, exp:
\title{**\vspace*{\fill}**[[title]]}
\date{[[date]]}
\author{[[author]]**\vspace*{\fill}**[[}
\begin{document}
\maketitle
\newpage
[[text]]
\end{document}
As in the answer by finrod, \maketitle is a pretty complicated macro, this is why I didn't feel like overwriting it myself (\renewcommand\maketitle{...). Nevertheless, copying, pasting and editing lines 170-201 of article.cls documentclass, I could add a new one to customize (\newcommand\mymaketitle{...) as follows:
\documentclass{article}
\setlength{\pdfpagewidth}{88.184mm}
\setlength{\pdfpageheight}{113.854mm}
\usepackage[margin=0.5cm, paperwidth=88.184mm, paperheight=113.854mm]{geometry}
\title{Title}
\date{Date}
\author{Author}
\makeatletter
\newcommand\mymaketitle{%
\begin{titlepage}
\null\vfil\vskip 40\p#
\begin{center}
{\LARGE \#title \par}
\vskip 2.5em
{\large \lineskip .75em \#author \par}
\vskip 1.5em
{\large \#date \par}
\end{center}\par
\#thanks
\vfil\null
\end{titlepage}
}
\makeatother
\begin{document}
\mymaketitle
Text
\end{document}
The output:

Resources