3

I defined a new command in order to create an empty box with a title. The code is the following:

  \newcommand{\mybox}[2]{
\textbf{\emph{#1}}
\vspace{2mm}
{\centering\fbox{
        \begin{minipage}{\textwidth}
            \hfill\vspace{#2}
        \end{minipage}
}   }
}

The problem is that when I used this exact same code without the new command I defined, the output was what I expected - a bold italic title and below that, an empty box.

However, when I use it with the new command, fox example \mybox{Title}{1in} the box is next to the title! It is very confusing!

I would appreciate your help, thank you very much in advance!

Torbjørn T.
  • 206,688
alexis
  • 33
  • 4
    Besides the direct answer already given, I would recommend to use a package like tcolorbox or mdframed to create a titled box. There are many examples here for both packages. http://tex.stackexchange.com/questions/169096/definitions-theorems-with-description-inside-a-box-and-the-title-above-the-b is quite similar to your box (plus a numbering which can be removed). – Thomas F. Sturm Apr 07 '14 at 13:42

1 Answers1

4

I can't really explain the technical reason I think, but add a \par after the title.

You also want to set the width of the minipage to \dimexpr\textwidth-2\fboxsep. This is because \fbox inserts some space between the frame and content. And finally, you need some % at the end of some lines, to avoid spurious spaces, see e.g. What is the use of percent signs (%) at the end of lines?, Why the end-of-line % in macro definitions? and Where are the necessary places to be appended with % to remove unwanted spaces?

\documentclass[12pt]{article}
\usepackage{showframe}
\newcommand{\mybox}[2]{%
\textbf{\emph{#1}}\par
\vspace{2mm}
{\centering\fbox{%
        \begin{minipage}{\dimexpr\textwidth-2\fboxsep-2\fboxrule\relax}
            \hfill\vspace{#2}
        \end{minipage}%
}}%
}
\begin{document}

\mybox{Title}{1in}

\end{document}

enter image description here

Torbjørn T.
  • 206,688