3

So I am sadly still not as versed with TeX as I wish I would be. I am looking for guidance on creating a custom command. I have been doing this manually so far but would want to take a step ahaed.

What I am trying to create is a command that would look something like \blank{anyword} which then creates me an underline with 1.5 times the length of the word. That would allow me, when I print that document to have my students fill out the blanks. The only problem I run into now, is that the command as-is is totally ignoring line-breaks and I know no solution to this. After googling like a madman I think there might be none.

I'll post a minimum working example which shows the problem. This is what I currently use, although this might not be the easiest solution:

\documentclass[a4paper,12pt]{article}
\usepackage{calc}
\newcommand{\gap}[1]{\rule{\widthof{#1}*\real{1.5}}{0.15mm}}

\begin{document}
    \setlength{\parindent}{0pt}
    
    anyword
    \vspace*{10mm}\\
    \gap{anyword}
    \vspace*{10mm}\\
    \textbf{Currently:}
    
    Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam \gap{nonumy-eirmod} tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.
    \vspace*{10mm}\\
    \textbf{How the Goal looks like:}
    
    Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam \gap{nonumy} \gap{eirmod} tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.
\end{document}

Do you have any insight? I would love the code to automatically do the \rule command only till the end of \textwidth and then make the rest of the gap in the next line of text.

Do you have any idea how to achieve this?

3 Answers 3

1

In a simple adaptation (see below for better approach), one could make the underline-gap of sub-segments, allowing a break between the sub-segments. Here, I break the underline-gap into 6 segments.

\documentclass[a4paper,12pt]{article}
\usepackage{calc}
\newcommand{\gap}[1]{%
  \rule{\widthof{#1}*\real{.25}}{0.15mm}\allowbreak%
  \rule{\widthof{#1}*\real{.25}}{0.15mm}\allowbreak%
  \rule{\widthof{#1}*\real{.25}}{0.15mm}\allowbreak%
  \rule{\widthof{#1}*\real{.25}}{0.15mm}\allowbreak%
  \rule{\widthof{#1}*\real{.25}}{0.15mm}\allowbreak%
  \rule{\widthof{#1}*\real{.25}}{0.15mm}%
}

\begin{document}
    \setlength{\parindent}{0pt}
    
    anyword
    \vspace*{10mm}\\
    \gap{anyword}
    \vspace*{10mm}\\
    \textbf{Currently:}

    Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam \gap{nonumy-eirmod} tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.
    \vspace*{10mm}\\
    \textbf{How the Goal looks like:}
    
    Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam \gap{nonumy} \gap{eirmod} tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua.
\end{document}

enter image description here

RECOMMENDED APPROACH

Taking this idea a step further, one could use a token cycle to literally set a rule for each character/space token in the input, which would allow for proper underlining of extended text passages.

\documentclass[a4paper,12pt]{article}
\usepackage{calc,tokcycle}
\newcommand{\gap}[1]{%
 \tokencycle
  {\rule{\widthof{##1}*\real{1.5}}{0.15mm}\hspace{0pt minus 2pt}}%
  {\processtoks{##1}}%
  {}%
  {\rule{\widthof{##1}*\real{1.5}}{0.15mm}\hspace{0pt minus 2pt}}%
  #1\endtokencycle
}

\begin{document}
    \setlength{\parindent}{0pt}
    
    anyword
    \vspace*{10mm}\\
    \gap{anyword}
    \vspace*{10mm}\\
    \textbf{Currently:}

    Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed 
diam \gap{nonumy-eirmod} tempor invidunt ut labore et dolore 
magna aliquyam erat, sed diam voluptua.

    Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed 
diam \gap{nonumy-eirmod nonumy-eirmod nonumy-eirmod nonumy-eirmod
nonumy-eirmod nonumy-eirmod nonumy-eirmod nonumy-eirmod
nonumy-eirmod nonumy-eirmod} tempor invidunt ut labore et dolore 
magna aliquyam erat, sed diam voluptua.

\end{document}

enter image description here

0

The \gap command will test whether the material is wider than 3.5em. If it's the case, it produces 1.75em wide rule, then as many copies of a very short rule (3pt) as are necessary to (approximately) fill the requested gap when followed by another 1.75em wide rule.

Between any two short rules a line break is allowed.

If the material is shorter than 3.5em just a non breakable rule is produced.

\documentclass{article}

\ExplSyntaxOn
\cs_new_eq:NN \replicate \prg_replicate:nn
\ExplSyntaxOff

\newlength{\gaplength}
\newcommand{\gap}[1]{%
  \settowidth{\gaplength}{#1}%
  \ifdim\gaplength<4em
    \leaders\hrule height -2pt depth 2.4pt\hskip\gaplength
  \else
    \splitgap
  \fi
}

\newcommand{\splitgap}{%
  \vrule height -2pt depth 2.4pt width 1.75em
  \replicate{%
    \fpeval{round((\gaplength-3.5em)/(3pt),0)}% how many times
  }{%
    \hbox to 3pt{\hss\vrule height -2pt depth 2.4pt width 3.1pt\hss}\penalty0
  }%
  \unpenalty
  \vrule height -2pt depth 2.4pt width 1.75em\relax
}

\begin{document}

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, 
sed diam nonumy-eirmod tempor invidunt ut labore et 
dolore magna aliquyam erat, sed diam voluptua.

Lorem ipsum dolor sit amet, \gap{consetetur} sadipscing elitr, 
sed diam \gap{nonumy-eirmod} tempor invidunt ut labore et 
dolore magna aliquyam erat, sed diam voluptua.

\end{document}

The line breaks are not exactly the same, because the “non-gap” version depends on the hyphenation of the words, which is not possible to take into account in the \gap version (well, it would require very substantial work).

enter image description here

0

Are you trying to reinvent dashundergaps?

With this package there are line breaks in gaps, but words are not hyphenated. However, but with the teach-mode you can control easily if some gap need a soft hyphen (\-) somewhere.

On the other hand, widening of gaps can be controlled using a larger font plus adding extra width in percentage. This percentage is not unlimited (maximum is around 40% or less, depending largely on the font used for gaps) but as setoff there are others interesting options.

Example:


mwe


\documentclass[a4paper,12pt]{article}
\usepackage{geometry,lipsum}\parskip1em % for the mwe only
\usepackage[%
    %%%% teacher-mode=true, % to see hidden text
]{dashundergaps}


\begin{document}
    
\lipsum[1][1-4] % text reference

% Gap without widening nor numbers

{\dashundergapssetup{%
gap-font=\normalsize, % default \bfseries 
gap-widen=false,
gap-number-format={}}
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit, \gap{vesti\-bulum} ut, placerat ac, adipiscing vitae, felis. \gap{Curabitur  dictum gravida mauris. Nam arcu libero, nonummy eget, consectetuer} id, vulputate a, magna.} 

% Default (widening for a bold font and numbers)  

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit, \gap{vesti\-bulum} ut, placerat ac, adipiscing vitae, felis. \gap{Curabitur  dictum gravida mauris. Nam arcu libero, nonummy eget, consectetuer} id, vulputate a, magna. 

% More widening and styles 

\dashundergapssetup{%
gap-widen=true,
gap-extend-percent=30, % default 20, maximum depends on the gap-font
gap-format=dash, 
gap-number-format= \textsuperscript{\tiny(\thegapnumber)}}    

Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit, \gap{vesti\-bulum} ut, placerat ac, adipiscing vitae, felis. \gap[.]{Curabitur  dictum gravida mauris. Nam arcu libero, nonummy eget, consectetuer} id, vulputate a, magna. 

\dashundergapssetup{%
gap-font=\Huge,
gap-extend-percent=0} % oppss!!decrease it with huge fonts ...


Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit, \gap[w]{vesti\-bulum} ut, placerat ac, adipiscing vitae, felis. \gap[d]{Cura\-bitur  dictum gravida mauris. Nam arcu libero, nonummy eget, consectetuer} id, vulputate a, magna. 


\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .