Text FormattingMeasure remaining length of line

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
rhino7890
Posts: 10
Joined: Fri Sep 24, 2010 7:27 pm

Measure remaining length of line

Post by rhino7890 »

I am using the package eforms as found in the AcroTex bundle. With it, I am trying to make an electronic form in which the user fills out text fields. With eforms, you can make interactive text boxes in LaTeX using the command \textField{<ID>}{<box width>}{<box height>}.

Where I run into trouble is the length of the text box. The way that I would like the form to look is as follows:

text1: _______________________________
text2: _______________________________
...
textN: _______________________________

where the underline section is where the interactive text field exists. As the command \textField requires an absolute length to specify the box width, I need some way of measuring the remaining length of the current line. Commands like \linewidth only return the current linewidth of the environment and not the remaining width of the current line. I also tried some variations of \hfill but without any luck. I even tried subtracting the length the leading text (by measuring it with the command \settowidth{\gnat}{text}) from the \linewidth value, but this was inconsistent from line to line.

Does anyone have any ideas on how I can get a text box to stretch the remaining length of the page?
Last edited by cgnieder on Thu Aug 29, 2013 1:53 pm, edited 3 times in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

gmedina
Posts: 2313
Joined: Wed Jul 11, 2007 11:45 pm

Measure remaining length of line

Post by gmedina »

Hi,

in the code below I defined a command \MyField that solves your problem:

Code: Select all

\documentclass{book}
\usepackage[pdftex]{eforms}

\newsavebox\mybox
\newlength\mylen
\newlength\fieldlen

% Syntax: \MyField{title-of-the-field}{text-before-the-field}
\newcommand\MyField[2]{%
  \sbox\mybox{#2}
  \settowidth\mylen{\usebox\mybox}
  \setlength\fieldlen{\linewidth}
  \addtolength\fieldlen{-\mylen}
  \addtolength\fieldlen{-1em}
  \par\noindent\usebox\mybox\hspace*{1em}\hfil\textField{#1}{\the\fieldlen}{20pt}\par\medskip
}

\begin{document}

\MyField{field1}{Text}

\MyField{field2}{Text two}

\MyField{field3}{Text three is longer}

\end{document}
Feel free to modify my example to suit your needs.
1,1,2,3,5,8,13,21,34,55,89,144,233,...
rhino7890
Posts: 10
Joined: Fri Sep 24, 2010 7:27 pm

Re: Measure remaining length of line

Post by rhino7890 »

Awesome! That did the trick. Thanks so much!
martin_gm
Posts: 1
Joined: Tue Apr 24, 2012 7:42 am

Measure remaining length of line

Post by martin_gm »

Just to clarify the herein provided solution:

You don't actually measure the remaining of the current line, you measure the beginning of the line and subtract it from \linewidth.

At least that's what the example code does. I don't know if there is actually a way to get the remainder of the line. I feel like it should be possible b/c LaTeX needs to know. How else could it produce the wonderful "overful \hbox" greetings?
tfrangakis
Posts: 1
Joined: Wed Aug 28, 2013 12:17 pm

Measure remaining length of line

Post by tfrangakis »

Hi all

I have been studying gmedina's code to try to solve a slightly different problem but with no answer yet - perhaps some kind soul out there can help me.

I am a lecturer and have an issue when compiling test and exam questions. The question looks something like this, where the marks shown in brackets are right justified on the last line of the problem:

Code: Select all

1. Determine the reactions and draw the shear force and bending moment diagrams.  \hfill(10 marks)
Using \hfill works well except where there is not sufficient space on the line to accommodate the marks bracket, in which case it wraps to the next line. I use (rather crudely) the code \\ \hfill\makebox{\hspace{13.3cm}(10 marks)} to get the mark allocation right justified on the succeeding line, but have to play with the hspace length to get it to look right.

Is there a way to measure the remaining space on a line and simultaneously measure the length of the marks bracket, then, in the case where the marks bracket length is larger than the remaining space get the marks bracket to right justify on the succeeding line. Note that the exact length of the marks bracket varies (consider the lengths of "(1 mark)" and "(20 marks)" and the exact text of the last line is not known until the text is compiled (as compared rhino7890's example where the exact text is known).

Thanks for your assistance.
Last edited by cgnieder on Thu Aug 29, 2013 1:47 pm, edited 1 time in total.
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Measure remaining length of line

Post by cgnieder »

Hi tfrangakis,

instead of adding a new post to a thread that has been marked as solved it would have been better to start a new thread. You could have referred to this thread by linking to it.

To me it seams you're setting various details of your exams by hand. Have you considered using a dedicated package or class such as exam, answers, probsoln or exsheets?

Regards
site moderator & package author
Post Reply