2

I'd like to typeset a checkbox, which one could simply do by using $\Box$. However, it should be part of a PDF form as well, which one could do by using the CheckBox command of hyperref.

Now, I need the combination of these two -- a box that's a visible and printable part of the "text" and the functionality as a PDF form.

When using a hyperref CheckBox, there is no box drawn in the PDF file, except for the box that's drawn as a part of the form, i.e. is drawn depending on the PDF viewer and not printed. I'd like to have a box as a part of the normal text with a hyperref CheckBox over it.

This should illustrate the problem:

\documentclass{article}
\usepackage{hyperref}
\usepackage{amssymb}
\parindent0pt

\begin{document}
\begin{Form}
\mbox{\CheckBox[width=10pt,height=10pt,bordercolor=0 0 0]{\null}} Option 1\\
$\Box$ Option 2\\
\fbox{\CheckBox[width=10pt,height=10pt,bordercolor=1 1 1]{\null}} Option 3
\end{Form}
\end{document}

The first "box" works as a PDF CheckBox and gets drawn by most PDF viewers as a black box; when printing, however, it vanishes, and some PDF viewers ignore the box.

The second box is just a plain box, which is printed in the required way but isn't a PDF form.

The third box is an fbox around a CheckBox, which is the desired result except that it has a weird shape and is too big -- reducing the dimensions of the CheckBox so the fbox gets small enough results in a not properly working (partially hidden) CheckBox.

Result of MWE

  • Why don't you use \fbox instead of \mbox? – Ulrike Fischer Sep 23 '17 at 21:43
  • @UlrikeFischer Thanks, I forgot to mention that. I've edited it. – magula Sep 23 '17 at 22:13
2

The default Checkbox field is formatted using the command \LayoutCheckField, and is defined like:

\newcommand\LayoutCheckField[2]{#1 #2}

Note how there is a space between the first and second argument.

You can remove this space (which fixes the odd rectangle shape), then set \fboxsep=0pt so the \fbox wraps around the Checkbox perfectly.

\documentclass{article}
\usepackage{amssymb}
\usepackage{hyperref}
\parindent0pt

\renewcommand\LayoutCheckField[2]{#1#2} % <------------
\fboxsep=0pt % <------------

\begin{document}

\begin{Form}
\mbox{\CheckBox[width=10pt,height=10pt,bordercolor=0 0 0]{\null}} Option 1\\
$\Box$ Option 2\\
\fbox{\CheckBox[width=10pt,height=10pt,bordercolor=1 1 1]{\null}} Option 3
\end{Form}
\end{document}

The output looks something like this in the PDF:

checkboxes

  • Using` {\ignorespaces}` for a label also works. – John Kormylo Sep 24 '17 at 16:06
  • @JohnKormylo Interesting. Though I think it's sufficiently different from mine to warrant its own place as a separate answer. :) – Troy Sep 24 '17 at 16:12
3

You need a unique name for \CheckBox to work. The default name is the same as the label, but in this case the label isn't valid.

Instead of redefining \LayoutCheckField one can use \ignorespaces in the label. Normally one would LIKE a space between the label and the check symbol.

Note that the checkbox border will overlap anything close to it, which is why the first and last checkboxes appear slightly to the left. Also, if you make the border white it will overwrite the \fbox.

At first I was skeptical for enclosing \CheckBox inside \mbox, but the last example demonstrates that otherwise the height will expand to match the rest of the line.

\documentclass{article}
\usepackage{xcolor}
\usepackage{hyperref}
\parindent=0pt

\begin{document}

\begin{Form}
\fboxsep=0pt
\fboxrule=0.9pt
\parskip=2pt
\mbox{\CheckBox[width=10pt,height=10pt,bordercolor=0 0 0,name=first]{\ignorespaces}} enclosed checkbox \par
\fbox{\textcolor{white}{\rule{10pt}{10pt}}} simulated checkbox \par
\fbox{\CheckBox[width=10pt,height=10pt,bordercolor=0 0 0,name=third]{\ignorespaces}} overlaid checkbox \par
\CheckBox[width=10pt,height=10pt,bordercolor=0 0 0,name=bonus]{\ignorespaces} \rule[-5pt]{1pt}{20pt} plain checkbox \par
\end{Form}
\end{document}

demo

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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