Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

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?

share|improve this question
add comment

3 Answers 3

up vote 10 down vote accepted

\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.

share|improve this answer
    
+1 for the alternative. I was doing all sort of weird stuff with forloops and whatnot. –  recluze Nov 5 '11 at 12:14
add comment

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.

enter image description here

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

\tikzstyle{shadedbox} = [
  text width=0.97\linewidth,
  draw=black,
  shade, top color=white, bottom color=white,
  drop shadow={
    top color=black,
    bottom color=black,
    shadow xshift=2pt,
    shadow yshift=-2pt,
  },
  rectangle,
]

\newcommand*\putansbox[2][]{%
  \noindent%
  \begin{tikzpicture}%
    \node [shadedbox, #1] (box) {%
    \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}
share|improve this answer
    
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
add comment

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}

\usepackage{adjustbox}

\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}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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