0

I have the problem that I need to typeset some text (in this case the string Solution:) and on the next line a centered image. I went with something like the following:

\documentclass[a4paper,10pt]{article}
\usepackage[utf8]{inputenc}
\usepackage[showframe]{geometry}

\begin{document}

\rule{1cm}{18cm} % Just here to simulate some text/images/...

Solution:
\nopagebreak

\begin{center}
\nopagebreak
\rule{5cm}{5cm}
\end{center}

\end{document}

The text Solution: is set in my installation on the first page with quite some space below it On the next page the 5cmx5cm block that contains the solution itself is set. I do not like this as it looks awkward.

Additionally, the underlying tex document is typeset twice (using \input), once only the exercises and once with the solutions. This makes it hard to manually add \pagebreak macros as it will break the exercise-only version.

The only way to get mostly what I want to achieve is to use

\begingroup
\vspace{1em}\centering
\rule{5cm}{5cm}
\par\vspace{1em}
\endgroup

This has some different spacing compared to the classical center environment. Of course, I could tune it yet a bit.

So: Is there a better way to get this done in LaTeX than to fall back to the TeX directive \centering?

2 Answers 2

2

Unfortunately, \nopagebreak does not override other favorable break-points inserted by other commands, as does the center environment. The regular way to prevent such breaks is with \samepage, but you still need \nopagebreak between paragraphs. So

\rule{1cm}{18cm} % Just here to simulate some text/images/...

\begin{samepage}
Solution:

\begin{center}
\rule{5cm}{5cm}
\end{center}

\end{samepage}

Note that commands like \samepage can be used as an environment to limit their scope. No \nopagebreaks were necessary here, but would be if there were any ordinary paragraph breaks included.

A better approach, though, is probably to use the needspace package, an declare \needspace{6cm} before the solution. This will make a ragged bottom page when needed.

1
  • The thing is that I do not know the height of the solution a priori as I want to create an environment that manages all the relevant parts. I think I will try with \samepage a bit unless there comes a better idea up. May 27, 2020 at 10:31
0

Without packages, use a minipage:

\documentclass[a4paper,10pt]{article}
\begin{document}
\rule{1cm}{18cm} 

\noindent\begin{minipage}{\linewidth}
Solution:
\begin{center}
\rule{5cm}{5cm}
\end{center}
\end{minipage}

\end{document}

Another simpler solution is use a header, even if you want "Solution" as normal text:

\documentclass[a4paper,10pt]{article}

\begin{document}

\rule{1cm}{18cm} 

\subsubsection*{\normalfont Solution:}

\begin{center}
\rule{5cm}{5cm}
\end{center}

\end{document}
2
  • The minipage approach will avoid any pagebreaks within it. So if the solution is lengthy, it cannot be broken accordingly, right? May 27, 2020 at 10:26
  • @ChristianWolf The minipage is unbreakable. For a large solution, you should include in the minipage the header "Solution:" and only one-two paragrapsh to avoid the widow header, al left the resto of the solution as normal text ... or you can convert "Sotution:" a true header (see the edited answer).
    – Fran
    May 27, 2020 at 11:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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