6

I am giving students ciphers/codes to solve. I want to add an underline under each individual letter with the underline far enough below the letter to allow the student to write the decoded letter.

I did this for one sentence but the time taken was too much for me to do this with all ciphers/codes. Is there a shorter way of accomplishing the underlining of individual letters while allowing space for writing a new letter?

\documentclass[a4paper,landscape]{article}
\usepackage{soul}
\usepackage[letterspace=150]{microtype}

\begin{document} 


\setul{48pt}{2pt}% 5pt below contents

\lsstyle

{\fontsize{40}{100}\selectfont \ul{S}\hspace{0.1cm}\ul{l}\hspace{0.1cm}\ul{a}\hspace{0.1cm}'\ul{z}\hspace{0.8cm}\ul{h}\hspace{0.1cm}\ul{s}\hspace{0.1cm}\ul{z}\hspace{0.1cm}\ul{v}\hspace{0.8cm}\ul{l}\hspace{0.1cm}\ul{e}\hspace{0.1cm}\ul{w}\hspace{0.1cm}\ul{y}\hspace{0.1cm}\ul{l}\hspace{0.1cm}\ul{z}\hspace{0.1cm}\ul{z}\hspace{0.8cm}\ul{n}\hspace{0.1cm}\ul{y}\hspace{0.1cm}\ul{h}\hspace{0.1cm}\ul{a}\hspace{0.1cm}\ul{p}\hspace{0.1cm}\ul{a}\hspace{0.1cm}\ul{b}\hspace{0.1cm}\ul{k}\hspace{0.1cm}\ul{l}\hspace{0.8cm}\ul{a}\hspace{0.1cm}\ul{v}\hspace{0.8cm}\ul{a}\hspace{0.1cm}\ul{o}\hspace{0.1cm}\ul{l}\hspace{0.8cm}\ul{b}\hspace{0.1cm}\ul{u}\hspace{0.1cm}\ul{z}\hspace{0.1cm}\ul{b}}


\end{document}

enter image description here

4 Answers 4

2

I split the input at spaces and then process each chunk. Such processing consists in examining whether the character is in the list of special ones (here apostrophe, comma and period) and if not a tabular is typeset; the letter is surrounded by some horizontal space.

After each letter a small space is added. Between every chunk a space is added.

Equal distancing between rows is obtained by locally setting a high value for \lineskip. Some more space between chunks is defined using \spaceskip.

\documentclass{article}
\usepackage{lipsum}

\ExplSyntaxOn
\NewDocumentCommand{\decode}{m}
 {
  \group_begin:
  \par\addvspace{\topsep}
  \dim_set:Nn \parindent { 0pt }
  \dim_set:Nn \lineskip { 2.5ex }
  \skip_set:Nn \spaceskip { 2em plus 0.5em minus 1em }
  \large
  \paul_decode:n { #1 }
  \par\addvspace{\topsep}
  \group_end:
 }

\seq_new:N \l__paul_decode_input_seq

\cs_new_protected:Nn \paul_decode:n
 {
  % split the input at spaces
  \seq_set_split:Nnn \l__paul_decode_input_seq { ~ } { #1 }
  % process each fragment
  \seq_map_function:NN \l__paul_decode_input_seq \__paul_decode_word:n
 }

\cs_new_protected:Nn \__paul_decode_word:n
 {
  \tl_map_function:nN { #1 } \__paul_decode_item:n 
  \linebreak[0]\ %
 }

\cs_new_protected:Nn \__paul_decode_item:n
 {
  \peek_regex:nTF { [',\.] } % the special characters
   { \__paul_decode_symbol:n } % there is a special character
   { \__paul_decode_letter:n } % no special character
   #1 % the letter
 }

\cs_new_protected:Nn \__paul_decode_symbol:n
 {
  \,#1\,\nolinebreak\hspace{0.2em}
 }

\cs_new_protected:Nn \__paul_decode_letter:n
 { 
  \begin{tabular}[t]{@{}c@{}} \,#1\, \\ \rule{0pt}{2ex}\hrulefill \end{tabular}
  \nolinebreak\hspace{0.2em}
 }

\ExplSyntaxOff

\begin{document}

\lipsum[1][1-4]

\decode{Sla'z hszv lewylzz nyhapabkl, av aol buzb.}

\lipsum[2][1-4]

\end{document}

enter image description here

1
  • It is a pleasure to read the clarity of your programming. An impressive source of progress. THANKS
    – pascal974
    Commented May 26, 2023 at 4:07
1

Based on this post which explains how to loop through strings, I'd say you are searching for:

\documentclass{article}
\usepackage{xparse}

\ExplSyntaxOn
\NewDocumentCommand{\markletters}{m}
 {
  % save the input in a variable
  \tl_set:Nn \l_kessels_unmarked_letters_tl { #1 }
  % replace spaces with \textvisiblespace
  \tl_replace_all:Nnn \l_kessels_unmarked_letters_tl { ~ } { \textvisiblespace }
  % map the input to underlined letters
  \tl_map_inline:Nn \l_kessels_unmarked_letters_tl { \hspace{0.1cm}\underline{##1} }
 }
\tl_new:N \l_kessels_unmarked_letters_tl
\ExplSyntaxOff

\begin{document}

\markletters{Hello World!}

\end{document}

and produced this:

enter image description here

Remarks:

  • Feel free to change the \hspace{0.1cm}. Without any space however you simply will get a seemingly connected line under all letters.
  • There is probably something more elegant that produces equally large lines under all letters.
  • you can get rid of \textvisiblespace by deleting it in line 10
1

With expl3 and tikz

the code

\documentclass{article}
\usepackage{tikz}
%https://tex.stackexchange.com/questions/686480/how-to-add-underlines-under-individual-letters-with-space-between-underline-and
\parindent=0pt
\ExplSyntaxOn
\int_new:N \l_pos_int
\int_set:Nn \l_pos_int {0}
\dim_new:N \l_rule_dim

\NewDocumentCommand{\vertstr}{ O{0.4pt} m }
{
  % #1 height of rule
  % #2 the text
  \begin{tikzpicture}
  % store argument as string
  \str_set:Nn \l_tmpa_str {#2}
  % traverse the string
  \str_map_inline:Nn \l_tmpa_str {
      \str_if_eq:nnTF { ##1 } { ~ }%  if space --> height of rule 0pt
        {\dim_set:Nn \l_rule_dim {0pt}}
        {\dim_set:Nn \l_rule_dim {#1}}
      % center each character at their own line
      % we can chage the distance in [4pt] by for example [0pt]
      \node[mynode] at (0.4*\l_pos_int,0){\centering \strut##1 \\[4pt] \rule{1em}{\l_rule_dim}};%
      \int_incr:N \l_pos_int
  }
  \end{tikzpicture}
}

\ExplSyntaxOff
\begin{document}
\tikzset{
  mynode/.style={
      %draw=black, 
      minimum height=1cm,
      text width=1em,
      inner sep=0pt
    }
}


\vertstr{Sla'z hszv lewyzz nyhapabkl}

\vertstr{av aol buzb}

\end{document}

enter image description here

0

Here I use a token cycle to achieve the goal. Note it can handle line and paragraph breaks, word wrapping, and only underlines cat 12 tokens.

\documentclass{article}
\usepackage{tokcycle}
\def\mystrut{\strut\rule[-15pt]{0pt}{0pt}}
\Characterdirective{\ifcat A#1
  \addcytoks{\underline{\mystrut#1}\nobreak\hspace{1pt}}\else
  \addcytoks{#1}\fi}
\Spacedirective{\addcytoks{#1\allowbreak}}
\begin{document}
\tokencyclexpress Sla'z hszv lewyzz nyhapabkl\endtokencyclexpress

\tokencyclexpress av aol buzb.\endtokencyclexpress

\tokencyclexpress Here is a test for manual line breaks\\
and also natural word wrapping 
and  natural word wrapping 
and also natural word wrapping 
and  natural word wrapping 
and also natural word wrapping 
and also natural word wrapping.\endtokencyclexpress
\end{document}

enter image description here

You must log in to answer this question.

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