What is the easiest way to draw a rectangle on latex? I just need a simple rectangle for my students to enter the final answer of a question and I am hoping I don't have to learn anything fancy like Tikz.
4 Answers
You could just do something simple like this
\documentclass{article}
\newcommand\answerbox{%%
\fbox{\rule{1in}{0pt}\rule[-0.5ex]{0pt}{4ex}}}
\pagestyle{empty}
\begin{document}
Put you answer here: \answerbox
\end{document}
which requires no use of TikZ
.
A bit of an explanation.
The syntax for \rule
is as follows:
\rule[<lift>]{<width>}{<height>}
I use two separate \rule
commands because if I wrote something like
\rule{1in}{4ex}
I just get a solid black box defeating the purpose.
By using a negative value for <lift>
I can drop the bottom of the box below the baseline.
\fbox
then adds more space. These values are controlled with the following lengths
\fboxrule
\fboxsep
Generally, \fboxrule
is 0.4pt
and \fboxsep
is 3pt
, but you can adjust those values as desired.
-
2It would be helpful if this answer explained what the adjustable parameters were. I think the 1in is the width and 4ex is the height. Is the -0.5ex how much to sink it below the baseline?– user6853May 13, 2018 at 16:32
-
Nothing fancy with tikz
. It is easy (this answer is for just showing it).
\documentclass{article}
\usepackage{tikz}
\begin{document}
Your answer: \tikz[baseline=0.6ex]\draw (0,0) rectangle (10cm,3ex);
Your answer: \tikz[baseline=-4pt]\node[draw,minimum width=10cm,minimum height=3ex] {};
\end{document}
-
I don't know why but when I type your code on my document, I get crazy slanted lines instead of a rectangle... (I hate tikz... :P) Nov 24, 2014 at 18:12
Actually, if all you want is a box for an answer, I'd say tikz might actually be the most readable (syntactically, at least) way to do this.
A box as wide as the text, 1in tall, is just:
\tikz \draw (0,0) rectangle (\linewidth, 1in);
The point (0,0) just references the current position in the text, then \linewidth
gives it the right width (which will adjust if you change your margins, and then just set the height, 1in
is just arbitrary.
Make sure to use \noindent
before beginning the picture, as it will try to indent the drawing and that'll cause weird alignment issues.
Here's a simple MWE:
\documentclass{article}
\usepackage{tikz}
\usepackage{lipsum}
\begin{document}
% question here
\lipsum[1]
\noindent\\
\tikz \draw (0,0) rectangle (\linewidth, 1in);
\end{document}
You could also use standard \begin{tikzpicture}
\end{tikzpicture}
brackets like with any other environment, but I used \tikz
instead because this drawing is only one line long. The draw command is one line long either way.
\documentclass{article}
\usepackage{tikz}
\usepackage{lipsum}
\begin{document}
\lipsum[1]
\noindent\\
\begin{tikzpicture}
\draw (0,0) rectangle (\linewidth, 1in);
\end{tikzpicture}
\end{document}
I know you asked for something other than tikz
, but I felt the same way as you until I finally took the plunge recently. The water's warm, I think you'll like it too.
The biggest advantage to using tikz
is being able to make a fancier answer box easily, for example, the line:
\tikz \draw[fill=green!5] (0,0) rectangle (\linewidth, -1in) node[pos=.1]{Answer Here:};
Gives you a 5% green fill and a label:
The draw option [fill=green!5]
fills the path with 5% green, and node[pos=.1]{Answer Here:}
makes a node (tikz
's awesome placement tool) 10% of the way into the box (it measures diagonally, pos=1
would set the text at the bottom right corner).
\documentclass{article}
\usepackage{tikz}
\usepackage{lipsum}
\begin{document}
\lipsum[1]
\noindent\\
\tikz \draw[fill=green!5] (0,0) rectangle (\linewidth, -1in) node[pos=.1]{Answer Here:};
\end{document}
Good luck!
This command draws a box filling the linewidth
(you could adjust that, of course), and allowing you to specify how tall the box is.
It creates a box of specified height using \raisebox
and then draws lines around it and specifies the width using \framebox
.
It uses an optional argument, so \answerbox
defaults to a height of 3\baselineskip
, but you can change it with \answerbox[0.5\textwidth]
or any other length.
\documentclass{article}
\newcommand{\answerbox}[1][3\baselineskip]{%
\noindent\framebox[\linewidth]{%
\raisebox{0pt}[0pt][#1]{}%
}\par\medskip%
}
\begin{document}
Write your answer here:
\answerbox
Write another answer here:
\answerbox[5\baselineskip]
\end{document}
\fbox{\phantom{This is how big an answer would be.}}
en.wikibooks.org/wiki/LaTeX/Boxes#framebox\fbox{\strut\rule{10cm}{0pt}}
.