# How to automatically determine the remaining width of the text field in this form?

I have this form made with the aid of hyperref package using the form environment:

\documentclass[pagesize=pdftex,DIV=16]{scrartcl}
\usepackage{xcolor}
\usepackage{hyperref}

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

\begin{document}
\begin{Form}
\section*{Principal Investigator}
\TextField[backgroundcolor=gray!10,borderwidth=0,width=.95\linewidth]{Full Name:}\\[1ex]
% \TextFieldFill[backgroundcolor=gray!10,borderwidth=0]{Full Name:}\\[1ex]
\TextField[backgroundcolor=gray!10,borderwidth=0,width=6cm]{Department:}
\TextField[backgroundcolor=gray!10,borderwidth=0,width=7cm]{Institute:}\\[1ex]
% \TextFieldFill[backgroundcolor=gray!10,borderwidth=0]{Institute:}\\[1ex]
\TextField[backgroundcolor=gray!10,borderwidth=0,width=.95\linewidth]{E-Mail:}\\
\end{Form}
\end{document}


Output:

as you can see, the space of the text fields: Full Name, Institute, and E-Mail do not align well at their right end.

Searching for a solution I stumbled over this post which solved this problem when there is only one text field sitting in one line like that of the Full Name and E-Mail, but not for the Institute. This half solution is provided in the MWE as commented code lines.

Question: How to let LaTeX automatically compute and apply the remaining space of the text field till the end of the linewidth for Institute as well as for Full Name and E-Mail?

• Perhaps it might help to look at how \hrulefill works. – musarithmia Feb 3 '16 at 16:22
• @AndrewCashner log file shows that: \hrulefill: macro:->\leavevmode \leaders \hrule \hfill \kern \z@, but I don't get it how can this be helpful in my case. – doctorate Feb 4 '16 at 16:27

\documentclass[pagesize=pdftex,DIV=16]{scrartcl}
\usepackage{xcolor}
\usepackage{hyperref}

\newsavebox\TBox

\begin{document}
\begin{Form}
\section*{Principal Investigator}
\sbox\TBox{Full Name: }%
\TextField[backgroundcolor=gray!30,borderwidth=0,width=\dimexpr\linewidth-\wd\TBox]{Full
Name:}\\[1ex]
\sbox\TBox{Department: }%
\TextField[backgroundcolor=gray!30,borderwidth=0,width=\dimexpr0.5\linewidth-\wd\TBox]{Department:}
\sbox\TBox{ Institute: }%
\TextField[backgroundcolor=gray!30,borderwidth=0,width=\dimexpr0.5\linewidth-\wd\TBox]{Institute:}\\[1ex]
\sbox\TBox{E-Mail: }%
\TextField[backgroundcolor=gray!30,borderwidth=0,width=\dimexpr\linewidth-\wd\TBox]{E-Mail:}
\end{Form}
\end{document}


• Great! your approach makes a lot of sense by subtracting the text before the field from the linewidth, but could you please explain to me why did you use \dimexpr0.5\linewidth instead of simply the \linewidth? and if possible a little bit account about this interesting TBox of thing. – doctorate Feb 4 '16 at 17:51
• There are two fields in that line! – user2478 Feb 4 '16 at 17:52
• I see, and what's the function of \newsavebox\TBox in the preamble? – doctorate Feb 4 '16 at 17:56
• It defines a box whch can be used to store text and then get the width of it \wd\TBox – user2478 Feb 4 '16 at 18:00
• So, \newcommand{\FormField}[1]{\sbox\Tbox{#1:\ }\TextField[backgroundcolor=gray!30,borderwidth=0,width=\dimexpr\linewidth-\wd\Tbox]{#1:\ }} – musarithmia Feb 4 '16 at 18:40

This does not answer your question, since you are making your form with the hyperref Form environment, which I don't understand. If this was as simple as filling a line with a grey box it would be pretty easy, after the model of LaTeX's \hrulefill. Perhaps this information might lead to a solution.

\hrulefill in LaTeX uses the TeX primitives \leaders and \hrule. \leaders fills a space with copies of a pattern, so \leaders\hrule fills the line with a horizontal rule. The \hfill\kern\z@ is necessary to tell leaders when to stop (in this case, at the end of the line).

You could easily get a visually similar result to your MWE just by defining a command that does the same thing but uses a grey box (technically, a "tall" \hrule) to fill the line. Because this uses TeX primitives anyway, here is a demonstration in Plain TeX (compile with pdftex).

\input color
\definecolor{fog}{gray}{0.85}