1

I would like to align the end of the TextFields in a Form such that they are in the same line. If there are only TextFields in a row, this question gives a good answer for me. What can I do if there is a CheckBox or other field, too?

\documentclass{article}
\setlength\parskip{1ex}
\usepackage{hyperref}
\usepackage{xcolor}
\newsavebox\TBox
\newcommand{\myTextField}[2]{%
    \sbox\TBox{#1 }%
    \TextField[backgroundcolor=gray!20,borderwidth=0,width=\dimexpr#2-\wd\TBox]{#1}%
}
\begin{document}
    \begin{Form}    
        \begin{center}
            \myTextField{Name:}{0.8\textwidth}

            \myTextField{Address:}{0.8\textwidth}


            \myTextField{Phone:}{0.5\textwidth}\hspace{0.01\textwidth}\myTextField{E-mail:}{0.29\textwidth}

            \CheckBox[bordercolor=black]{Speaker:}\hspace{0.05\textwidth}\myTextField{Lecture title:}{0.608\textwidth}
        \end{center}
    \end{Form}
\end{document}

enter image description here

bmv
  • 3,588

1 Answers1

4

It would be easiest to mark the x-coordinate on the page of the left side of your Lecture title: \TextField and the right side of the other components above it (that should be at the 0.8\textwidth mark, barring an indent) and then use that to calculate the horizontal width:

enter image description here

\documentclass{article}

\setlength{\parskip}{1.5ex}

\usepackage{xcolor,zref-savepos}
\usepackage{hyperref}
\newsavebox\TBox
\newcommand{\myTextField}[2]{%
  \sbox\TBox{#1 }%
  \TextField[backgroundcolor=gray!20,borderwidth=0,width=\dimexpr#2-\wd\TBox]{#1}%
}
\begin{document}

\begin{Form}    
  \myTextField{Name:}{0.8\textwidth}

  \myTextField{Address:}{0.8\textwidth}

  \myTextField{Phone:}{0.5\textwidth}%
  \hspace{0.01\textwidth}%
  \myTextField{E-mail:}{0.29\textwidth}%
  \zsaveposx{x-right}% Mark right x-coordinate

  \CheckBox[bordercolor=black]{Speaker:}%
  \hspace{0.05\textwidth}%
  \zsaveposx{x-left}% Mark left x-coordinate
  \myTextField{Lecture title:}{\dimexpr\zposx{x-right}sp-\zposx{x-left}sp}
\end{Form}

\end{document}

zref's savepos module makes it easy to capture the coordinates (x, y or both) and use them in calculations. \zsaveposx captures the x-coordinate as a "\label" while \zposx retrieves the coordinates in scaled points (or sps) relative to the page geometry; (0,0) is the bottom-left corner.

Werner
  • 603,163