2

Code

% https://tex.stackexchange.com/a/159996/13173
\documentclass{article}
\usepackage[normalem]{ulem}
\begin{document}
\begin{enumerate}
\item Initials: \uline{A.B.C.} 
\item Age: \uline{50}
\item Address: \uline{Tampere}
\item Condition: \uline{ % showing where \uline{ \hfill\null}} 

      Entered text here in many lines. % I have also here many comments which causes problems

      It can go on many lines. % commments can be here

      \hfill\null}
\end{enumerate}
\end{document}

Fig. 1 Current output, Fig. 2 Complication of overloading text at the right-hand-side

enter image description here

enter image description here

Wished output: underline goes all the linewidth but does not pass the right-hand-side margin

Output in TeXLive 2017 because of comments in ulem environment

(/usr/local/texlive/2017/texmf-dist/tex/generic/ulem/ulem.sty) (./test.aux)
Runaway argument?
\@empty 
! Paragraph ended before \UL@word was complete.
<to be read again> 
                   \par 
l.15       \hfill\null}

? 
! Emergency stop.
<to be read again> 
                   \par 
l.15       \hfill\null}

Testing Ian's answer

Please, see Fig. 2 about the complication where the underlined text passes through the right-hand-side margin.

OS: Debian 8.7 64 bit
TeXLive: 2017
Engine: Pdflatex
Hardware: Asus Zenbook UX303UA

Johannes_B
  • 24,235
  • 10
  • 93
  • 248
  • I'm not quite sure what you want now. If the text is split over multiple lines, should the underline continue to the end of the last line? What if there is no space at the end of the last line? – Ian Thompson Oct 04 '16 at 11:18
  • @IanThompson Yes, if the text continues to the next line, you should underline both lines to the end. If there is no space at the end of the last line, then I think the case should consider if there is newline or end of the current \item object. - - What kind of difficulties can you run if you do not have a space at the end? - - The test condition which should be met is that you should not pass the right-hand-side margin with underline and text as in Fig. 2. – Léo Léopold Hertz 준영 Oct 04 '16 at 11:20
  • 1
    You cannot have a blank line in \uline just as you cannot have a blank line in \textsc or in section. – Johannes_B Jun 11 '17 at 10:52
  • 1
    Besides the fact that underlining text in printed work is highly discouraged (underlining is a substitute for handwritten notes, as students have a ruler handy), it is a pain with LaTeX, as linebreaking underlined text isn't nice. – Johannes_B Jun 11 '17 at 10:56
  • Out of interest, do you see the overflow without underlining? – Johannes_B Jun 11 '17 at 11:00

3 Answers3

4

Like this?

\documentclass{article}
\usepackage[normalem]{ulem}
\begin{document}
\begin{enumerate}
\item Initials: \uline{A.B.C.\hfill\null}
\item Age: \uline{50\hfill\null}
\item Address: \uline{Tampere\hfill\null}
\end{enumerate}
\end{document}
Ian Thompson
  • 43,767
2

What about this?

\documentclass{article}
\usepackage[normalem]{ulem}



\begin{document}
\newcommand*{\newlinecommand}[2]{%
  \newcommand*{#1}{%
    \begingroup%
    \escapechar=`\\%
    \catcode\endlinechar=\active%
    \csname\string#1\endcsname%
  }%
  \begingroup%
  \escapechar=`\\%
  \lccode`\~=\endlinechar%
  \lowercase{%
    \expandafter\endgroup
    \expandafter\def\csname\string#1\endcsname##1~%
  }{\endgroup#2\par\space}%
}

%%% USAGE:
\newlinecommand{\myunder}{\uline{#1\hfill\null}}

\begin{enumerate}
\item First words \myunder rest Of line 
some More text not underlined
\item \myunder test 
%comment here
\myunder test2 on other line underlined %comment before empty line

test3 after paragraph not underlined
\item \myunder test3 befor a comment %comment

\myunder continue in new paragraph underlined
\item \myunder test4
%coment again
\myunder last line underlined
\end{enumerate}

\end{document}

it produces this: this

and I have used this https://tex.stackexchange.com/questions/1042/def-taking-rest-of-the-line-as-argument answer... I think it is more comfortable to work with this way even if it is not exactly what you are looking for (because you need new underline command in every line -but I think this is good-)

koleygr
  • 20,105
  • Can you please explain little about the code before the newlinecode definition? I am thinking how you apply it to the command myunder. – Léo Léopold Hertz 준영 Jun 16 '17 at 13:42
  • 1
    The explanation is in the link above (where I found how to read a line until find newline)... This is what it does with a really clever way (as far as I can understand)... The thing I understood is that the \newlinecommand names #1 all the string that follows the command we choose (here the \myunder) until the next end of line... You may understand more from the url above (may be asking there is a good idea... they sure know more) – koleygr Jun 16 '17 at 15:30
  • I awarded this answer the bounty because it answers my question. - - I think that the main script can still be improved, the one from the other thread. – Léo Léopold Hertz 준영 Jun 18 '17 at 07:15
  • 1
    Thank you @Léo Léopold Hertz 준영... wow!... too many points!... I tried to improve the answer and make it work with braces... but I had no luck... If I find a way to improve the answer I will update the post (I think the main script is just too clever for me to understand exactly... but it works fine....) – koleygr Jun 18 '17 at 07:22
0

Remove comments inside \uline{...} environment. Otherwise, same approach as in the body. I think the environment does not support them.

Heiko's proposal with soul's ul

Code

\documentclass{article}
\usepackage{soul}
\begin{document}
\begin{enumerate}
\item Initials: \ul{A.B.C.}
\item Age: \ul{50}
\item Address: \ul{Tampere}
\item Condition: \ul{ % showing where \uline{ \hfill\null}} 

      Entered text here in many lines. % I have also here many comments which causes problems

      It can go on many lines. % commments can be here

      \hfill\null}
\end{enumerate}
\end{document}

Output in Fig. 1 not completing the end of line requirement but works on paragraphs (comments) + an empty line is considered an empty line which should not be the case: single _

Fig. 1 Output

enter image description here

Skillmon
  • 60,462