# Defining a width that fills remaining horizontal space for text fields in hyperref forms environment

This code

\documentclass[10pt,a4paper]{article}

\usepackage{xcolor}
\usepackage{hyperref}

\begin{document}
\begin{Form}
\noindent\textbf{DESCRIPTION OF ACTIVITY}\raisebox{-2pt}{\TextField[name=description,width=4.3in]{~}}
\end{Form}

\end{document}


gives the output

Notice that the text field box did not end at the right margin. This results to ugly alignment of the boxes after typing several lines with text fields. I could have used \hfill before the text field but that will leave ugly white space between DESCRIPTION... and the text field.

So, to my question: How can I define a width, remwidth, such that I can write

\noindent\textbf{DESCRIPTION OF ACTIVITY}\raisebox{-2pt}{\TextField[name=description,width=remwidth]{}}


so that the text field box stops exactly at the right margin rule?

I would appreciate it very much if the the answer would include an explanation of what is happening at every step.

Edit: October 15, 2012 I edited the question since the problem is really about the \TextField command and the hyperref package putting a padding in it as noted in the comments. The other part of my question on defining a width command that I can use elsewhere is actually solved by \linewidth, which I just learned last night even after 3 years of using LaTeX. The question Difference between \textwidth, \linewidth and \hsize clarified its use for me.

-

The mandatory argument to \TextField is the text that precedes the box. It's sufficient to measure it (and cut out another

\documentclass{article}
\usepackage[pass,showframe]{geometry}
\usepackage{lipsum}
\usepackage{hyperref}

\newlength\TextFieldLength
\newcommand\TextFieldFill[2][]{%
\setlength\TextFieldLength{\linewidth}%
\settowidth{\dimen0}{#2 }%
\TextField[#1,width=\TextFieldLength]{\raisebox{2pt}{#2 }}%
}

\begin{document}
\lipsum[2]

\begin{Form}
\noindent\TextFieldFill[name=description]{\textbf{DESCRIPTION OF ACTIVITY}}

\noindent\TextFieldFill[name=procrastination]{\textbf{WAYS TO PROCRASTINATE}}

\end{Form}
\end{document}


The lipsum and geometry packages are just to provide text and the frame. The 2.22221pt excess has been determined from the Overfull \hbox message that one gets without it.

One can get rid of this adjustment by doing

\newcommand\TextFieldFill[2][]{%
\setlength\TextFieldLength{\linewidth}%
\settowidth{\dimen0}{#2 }%
\TextField[#1,width=\TextFieldLength]{\raisebox{2pt}{#2}}%
}


because the macro that is called by \TextField adds one between the text and the box (thanks to Heiko Oberdiek for pointing out this).

-
+1 for WAYS TO PROCRASTINATE. i was about to sleep when i saw that and laughed hard :D –  hpesoj626 Oct 14 '12 at 15:33
@hpesoj626 You can see that the field is wider than the one for the description. :-) –  egreg Oct 14 '12 at 15:35
@egreg An additional space is added by \LayoutTextField, then \dimen0 if fixed by \settowidth{\dimen0}{#2 \space} and you can get rid of the 2.2221pt that also might change depending on the glue of the text description. –  Heiko Oberdiek Oct 15 '12 at 2:27
@HeikoOberdiek, Tex does not warn of an overfull \hbox with your suggestion but the end of the text field will cross the right margin with showframe option in geometry package. Why is that? –  hpesoj626 Oct 15 '12 at 6:11
Some hyperref drivers add a link margin of 1pt: \addtolength\TextFieldLength{-1pt}. See also \ref with tight box surrounding the reference. –  Heiko Oberdiek Oct 15 '12 at 11:34

It's easiest if you measure the earlier text then you can subtract its width from \linewidth

\documentclass[10pt,a4paper]{article}

\usepackage{xcolor}
\usepackage{hyperref}
\newbox\formbox

\begin{document}
\begin{Form}
\noindent
\setbox\formbox\hbox{\textbf{DESCRIPTION OF ACTIVITY}}\usebox\formbox
\raisebox{-2pt}{\TextField[name=description, width=\dimexpr\linewidth-\wd\formbox\relax]{}}
\end{Form}

\end{document}

-
This produces an overfull \hbox`. –  Gonzalo Medina Oct 14 '12 at 14:42
This will require me to type \setbox\formbox\hbox... in every line though. Even with copy and paste, this becomes tedious after several lines :) –  hpesoj626 Oct 14 '12 at 14:43
I don't know why but it does not work for me. –  Sigur Oct 14 '12 at 14:45
@hpesoj626 using David's idea you can define a two-argument command to do the work for you. –  Gonzalo Medina Oct 14 '12 at 15:21
@GonzaloMedina yes I suspect hyperref is padding the box, but checking where would require reading the manual. But it seems egreg has read the manual already. –  David Carlisle Oct 14 '12 at 17:36