# New command for a height-flexible rect

I'm trying to create a command that will take in one argument and create a rectangle around that number of empty lines. I have the following:

\newcommand{\putansbox}[1]{
\framebox[\textwidth]{
\vspace{\the\baselineskip}
}
}


It only creates one line though. How can I use the #1 in here?

-

\framebox is a horizontal box, so you cannot add vertical space to it, without using a vertical box like \parbox inside. Here is another solution with an invisible rule:

\documentclass{article}

\newcommand{\putansbox}[1]{%
\framebox[\textwidth]{\rule{0pt}{#1\baselineskip}}
}

\begin{document}
\noindent\putansbox{5}
\end{document}


And if you don't want the box to break the line width, you may use, e.g.

\newcommand{\putansbox}[1]{%
\framebox[\dimexpr \linewidth-2\fboxrule-2\fboxsep\relax]{%
\rule{0pt}{#1\baselineskip}%
}%
}


To not forget the answer to your question: You may use the #1 as a prefix of \baselineskip because \baselineskip is a length and lengths may be used like units.

-
+1 for the alternative. I was doing all sort of weird stuff with forloops and whatnot. – recluze Nov 5 '11 at 12:14

If you want to get fancier, you could also use something like mdframed, or tikz.

Here is a tikz solution using shadedbox from How to right align a table in a tikz picture, so if you like this solution, please up vote that answer.

The \putansbox defined here accept an optional first parameter that provides you the option of using the full power of tikz to modify what you want. Refer to the TikZ documentation for more details and options, but the options that are shown in this MWE include:

• specify a top/bottom color for the shading: bottom color=yellow!10, top color=red!10
• using rounded corners: rounded corners=15pt
• adjusting the line width: line width=1pt
• adjusting the line color: draw=blue
• adjusting the width of the box: text width=0.6\linewidth
• adjusting the line style: densely dotted

See the TikZ documentation for more options.

\documentclass[border=5pt]{standalone}
\usepackage{calc}
\usepackage{xcolor}
\usepackage{tikz}

text width=0.97\linewidth,
draw=black,
top color=black,
bottom color=black,
},
rectangle,
]

\newcommand*\putansbox[2][]{%
\noindent%
\begin{tikzpicture}%
\vspace {#2\baselineskip}%
};%
\end{tikzpicture}\\%
}

\begin{document}

\putansbox{5}

\putansbox[bottom color=yellow!10]{5}

\putansbox[rounded corners=15pt, line width=1pt, draw=blue]{5}

\putansbox[text width=0.6\linewidth, top color=red!10, densely dotted]{5}
\end{document}

-
Thanks ... I guess this is a little over my head and also a little bit of an overkill. I'm designing this for print and want to keep it simple. Good intro to tikz though. – recluze Nov 6 '11 at 10:05

Here a solution based on \phantombox{<width>}{<height>}{<depth>} from my adjustbox package. It creates an empty box with the given dimensions, then use \frame to draw a tight frame around it. I also added some other macros to add some separation between the surrounding text.

\documentclass{article}

\newcommand{\putansbox}[1]{%
\par\medskip\par\noindent
\frame{\phantombox{\linewidth}{#1\baselineskip}{\dp\strutbox}}%
\par\smallskip\par
}

\begin{document}

text
\putansbox{5}
text

\end{document}

-