# Preventing \mbox or \fbox from exiting the page

I am trying to do something like this:

\documentclass[a4paper]{article}

\usepackage[top=0mm, bottom=0mm, left=0mm, right=0mm]{geometry}

\begin{document}

\noindent
\fbox{\hskip1cm Some text \hskip1cm}\hspace{1cm}\allowbreak%
\fbox{\hskip1cm Some text \hskip1cm}\hspace{1cm}\allowbreak%
\fbox{\hskip1cm Some text \hskip1cm}\hspace{1cm}\allowbreak%
\fbox{\hskip1cm Some text \hskip1cm}\hspace{1cm}\allowbreak%
\fbox{\hskip1cm Some text \hskip1cm}\hspace{1cm}\allowbreak%
\fbox{\hskip1cm Some text \hskip1cm}\hspace{1cm}\allowbreak%
\fbox{\hskip1cm Some text \hskip1cm}

\end{document}


The document(s) will be automatically generated, so I want to avoid adding newlines by hand, or putting this in a table.

What I want is to make a bunch of nametags with given width and height (I've solved that part) and put them in a document. It should be up to LaTeX to decide when they get out of the page and insert a new line, like it does with text. Unfortunatelly, my boxes run out of the page.

In the above example, I get five boxes in a row, but the fifth one is out of the page, so it should actually be the first one in the second row (i.e., I should get four per row, not five).

I am aware that I might be looking in a completely wrong direction, so suggestions on the alternative ways to make nametags are also welcome. However, my code currently works as intended (apart from this glitch), so I'd prefer just fixing this.

• Use \sloppy in your preamble. Also, using \usepackage[margin=0pt]{geometry} is similar to your setting of all margins to 0mm. – Werner Sep 23 '13 at 16:06
• @Werner Very simple and works, thank you. Please, put it as an answer, so I can accept it. As for my margins, they are configurable; the above is just an example. – Vedran Šego Sep 23 '13 at 16:07
• Note \allowbreak isn't doing anything here but if it is used it should be before the space, not after it, as if you break there you want the space to go to the next line and be discarded. – David Carlisle Sep 23 '13 at 16:11
• @DavidCarlisle Thank you for pointing this out. Along with either Werner's or David's answers, it is not needed. I obviously misunderstood what it does. – Vedran Šego Sep 23 '13 at 16:14

## 2 Answers

Your \hspace had no flexibility so you either need to change \textwidth to be exactly right or simpler use \raggedright:

\documentclass[a4paper]{article}

\usepackage[top=0mm, bottom=0mm, left=0mm, right=0mm]{geometry}

\begin{document}
\raggedright

\fbox{\hskip1cm Some text \hskip1cm}\hspace{1cm}%
\fbox{\hskip1cm Some text \hskip1cm}\hspace{1cm}%
\fbox{\hskip1cm Some text \hskip1cm}\hspace{1cm}%
\fbox{\hskip1cm Some text \hskip1cm}\hspace{1cm}%
\fbox{\hskip1cm Some text \hskip1cm}\hspace{1cm}%
\fbox{\hskip1cm Some text \hskip1cm}\hspace{1cm}%
\fbox{\hskip1cm Some text \hskip1cm}

\end{document}


The following seems to be in line with what you're after:

\documentclass{article}
\usepackage[a4paper,margin=0mm]{geometry}% http://ctan.org/pkg/geometry
\usepackage{tikz}% http://ctan.org/pkg/pgf
\raggedright
\begin{document}

\foreach \name in {Tom, Dick, Harry, Mary, Jane, Sue, Sylvester} {
\framebox[4cm][c]{\strut\name}\qquad%
}
\end{document}


It provides a more consistent way of typesetting the nametags. Line-breaking is provided by means of \raggedright, while a \strut ensures even \framebox height across the nametags. Since the setting is done in a loop (with \foreach or something similar), you merely have to change the names in the comma-separated list.

• +1 but you could lose the \centering (as it does nothing in an hbox) – David Carlisle Sep 23 '13 at 16:20
• Thank you. It is a nice solution, but my system already has two other kinds of output (one HTML, and one TeX), so I'm looking only to upgrade it to be able to do name tags as well. – Vedran Šego Sep 23 '13 at 17:57