5

I am typesetting a mnemonic for remembering values of pi. For this I want the number of characters in the word to appear on the next line aligned with where word starts. I have tried both tabular and align but both of them do not preserve the spacing in the sentence structure as shown in MWE. The tabular gives sightly better results. Is there a way to typeset this so that sentence appears as in regular text and the numbers get alinged with them in the next line?

enter image description here

What is required:

enter image description here

MWE

\documentclass{article}

\usepackage{amsmath}


\begin{document}

\begin{align*}
\text{May} & \text{I} & \text{have} & \text{a} & \text{large} & \text{container} & \text{of} & \text{coffee?} \\
3   & 1 & 4    & 1 & 5     & 9         & 2  & 6
\end{align*}

\begin{tabular}{llllllll}
May & I & have & a & large & container & of & coffee? \\
3   & 1 & 4    & 1 & 5     & 9         & 2  & 6\\
\end{tabular}


\end{document}
0

6 Answers 6

2

You only need the package dvgloss and use \gl{May I have}{3 1 4}. A not so minimal example:

Mwe

\documentclass{article}
\usepackage[margin=7cm]{geometry}
\usepackage{dvgloss,xcolor}
\setlength\parindent{0pt}
\def\everygla{\sffamily\Large\color{cyan!40!blue}}
\def\everyglb{\sffamily\hfil\footnotesize}
\begin{document}
\gl{May  I  have a  large  * container * of coffee? 
Maybe  one  round  recepter * traslucid * fuschia available now}
{3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3}
\end{document}
1
  • Thanks, for all the other excellent responses. I learnt a lot. I am choosing this as answer as it also centres the numbers with words (thought it was not part of original requirements).
    – Damitr
    yesterday
6

There is also the tabbing environment that allows to set tab stops with \= and move to the next tab stop with \>.

Example:

\documentclass{article}

\begin{document}

\noindent
May  I  have  a  large  container  of  coffee?

\begin{tabbing}
May \= I \= have \= a \= large \= container \= of \= coffee? \\
3   \> 1 \> 4    \> 1 \> 5     \> 9         \> 2  \> 6\\
\end{tabbing}

\end{document}

Example with the tabbing environment

5

This won't work with accented characters.

\documentclass{article}
\usepackage{lipsum}

\ExplSyntaxOn
\NewDocumentCommand{\mnemonic}{O{quote}m}
 {
  \begin{#1} \setlength{\lineskip}{4pt}
  \seq_set_split:Nnn \l_tmpa_seq { ~ } { #2 }
  \seq_map_function:NN \l_tmpa_seq \__damitr_pi_word:n
  \end{#1}
 }
\cs_new_protected:Nn \__damitr_pi_word:n
 {
  \tl_set:Nn \l_tmpa_tl { #1 }
  \regex_replace_all:nnN { [^[:alpha:]] } { } \l_tmpa_tl
   {
    \begin{tabular}[t]{@{}l@{}}
    \cs_set:Npn \\ {} #1 \tabularnewline
    \tl_count:N \l_tmpa_tl
    \end{tabular}
    \exp_args:Ne \str_if_eq:nnTF { \tl_range:nnn { #1 } { -1 } { -1 } } { \\ } { \\ } { ~ }
   }
 }
\ExplSyntaxOff

\begin{document}

\lipsum[1][1-4]
\mnemonic{May I have a large container of coffee?}
\lipsum[2][1-4]
\mnemonic[verse]{I wish I could calculate pi,\\
"Eureka!" cried the great inventor,\\
Christmas pudding, Christmas pie,\\
Is the problem's very center.}
\lipsum[3][1-4]

\end{document}

Caveat: a \\ denoting a line break must be attached to the preceding word.

enter image description here

2
  • I wanted to make your code work with accents,... I had difficulties, but it allowed me to progress with Expl3.
    – pascal974
    20 hours ago
  • @pascal974 It might work with a Unicode engine (LuaTeX or XeTeX). For pdftex a much slower routine could be devised.
    – egreg
    20 hours ago
4

To get dots as spaces as shown in your image, you can use verbatim* environment with redefining \verbvisiblespace, e.g.:

\documentclass{article}

\DeclareRobustCommand\verbvisiblespace
           {\leavevmode{\usefont{OT1}{cmtt}{m}{n}\raisebox{.5ex}{.}}}

\begin{document}
\begin{verbatim*}
May I have a large container of coffee?
3   1 4    1  5    9         2  6     
\end{verbatim*}
\end{document}

without delimiter

However, as you see, there would be a problem with automatically removing of spaces at the end of line. To avoid this, you would need to have an extra delimiter, e.g.

\documentclass{article}

\DeclareRobustCommand\verbvisiblespace
           {\leavevmode{\usefont{OT1}{cmtt}{m}{n}\raisebox{.5ex}{.}}}

\begin{document}
\begin{verbatim*}
May I have a large container of coffee?
3   1 4    1  5    9         2  6     |
\end{verbatim*}
\end{document}

with delimiter or

\documentclass{article}

\DeclareRobustCommand\verbvisiblespace
           {\leavevmode{\usefont{OT1}{cmtt}{m}{n}\raisebox{.5ex}{.}}}

\AddToHook{cmd/@setupverbvisiblespace/after}{%
  \catcode`\|=\active
  \verbunvisiblebar
}
\begingroup
  \catcode`\|=\active
  \gdef\verbunvisiblebar{\def|{\mbox{}}}%
\endgroup
           
\begin{document}
\begin{verbatim*}
May I have a large container of coffee?
3   1 4    1  5    9         2  6     |
\end{verbatim*}
\end{document}

enter image description here

A smart implementation that does the digit counting automatically would also be possible:

\documentclass{article}

\ExplSyntaxOn
\seq_new:N \l_wtd_words_seq
\NewDocumentCommand \wordstodigits { O { \raisebox{.5ex}{.} } m O{} }
  {
    \begin{flushleft}
      \seq_set_split:Nnn \l_wtd_words_seq { ~ } { #2 }
      \texttt { \seq_use:Nn \l_wtd_words_seq { #1 } #3 }
      \par
      \int_set:Nn \l_tmpb_int { \seq_count:N \l_wtd_words_seq }
      \seq_map_inline:Nn \l_wtd_words_seq
        {
          \int_set:Nn \l_tmpa_int { \str_count:n { ##1 } }
          \texttt { \int_use:N \l_tmpa_int }
          \int_decr:N \l_tmpa_int
          \int_decr:N \l_tmpb_int
          \int_step_inline:nn { \l_tmpa_int } { \texttt { #1 } }
          \int_if_zero:nF { \l_tmpb_int } { \texttt { #1 } }
        }
      \par
    \end{flushleft}
  }
\ExplSyntaxOff
           
\begin{document}
\wordstodigits{May I have a large container of coffee}[?]
\end{document}

smart result

2
  • 1
    did you mean to surround the code with two \ExplSyntaxOn ? rather than off? probably more natural to have \ExplSyntaxOff then \wordstodigits{May I have a large container of coffee}[?] with spaces not tilde May 14 at 18:06
  • @DavidCarlisle Thank you for illuminating my blindness.
    – cabohah
    2 days ago
3

For tabular if you want word spaces between the columns use *{10}{l@{ }} or similar but here I think I would not use an outer structure at all, but just construct the list of words so that if you gave longer sentences they can still take part in linebreaking and be justified by stretching space as needed.

enter image description here

\documentclass{article}

\newcommand\mnem[2]{\begin{tabular}[t]{@{}l@{}}#1\\#2\end{tabular}}
\begin{document}

\mnem{May}{3.} \mnem{I}{1} \mnem{have}{4} \mnem{a}{1} \mnem{large}{5} \mnem{container}{9} \mnem{of}{2} \mnem{coffee?}{6}

\end{document}
3

From egreg's answer, (I'm French) and I wanted to make it work with accents and different management of the apostrophe.

  • I also added a test to spot errors.
  • I use babel to manage spaces for punctuation
  • lualatex

Code

% !TeX TS-program = lualatex
\documentclass{article}
\usepackage[french,english]{babel}%<-- added
\usepackage{lipsum}
\usepackage{color}%<< To spot errors
\ExplSyntaxOn
%
%  egreg'code with modif
%   --> accent
%   --> apostrophe in french
%
\tl_new:N \l_pi_decimales_tl
\int_new:N \l_pi_posdecimales_int
\bool_new:N \l_pi_apostophe_bool
\bool_set_false:N \l_pi_apostophe_bool
\NewDocumentCommand{\apostrophe}{ m }
{
  \int_compare:nNnTF {#1} = {0}
    {\bool_set_false:N \l_pi_apostophe_bool}
    {\bool_set_true:N \l_pi_apostophe_bool}
}
\NewDocumentCommand{\mnemonic}{O{quote} m}
{
  %%%%%%%%%%%%% Added
  \tl_set:Nn \l_pi_decimales_tl{31415926535897932384626433832795028841971693993751058209749445923078164062862
089986280348253421170679821480865132823066470938446095505822317253594081284811}
  \int_set:Nn \l_pi_posdecimales_int {1}
%%%%%%%%%%%%%%%%%%%%%%%%%
  \begin{#1} \setlength{\lineskip}{4pt}
  \seq_set_split:Nnn \l_tmpa_seq { ~ } { #2 }
  \seq_map_function:NN \l_tmpa_seq \__damitr_pi_apostrophe:n
  \end{#1}
}

\cs_new_protected:Npn \__damitr_pi_word:nn #1#2
% #1: the word
% "2: the symbol after -> space or ' 
{  
  \tl_set:Ne \l_tmpa_tl { #1 }
  \regex_replace_all:nnN { [àèéÔ] } { a } \l_tmpa_tl%<-- added
  \regex_replace_all:nnN { [œ] } { aa } \l_tmpa_tl%<
  \regex_replace_all:nnN { [^[:alpha:]] } { } \l_tmpa_tl
  %  { % ?
    \begin{tabular}[t]{@{}c@{}}
    \cs_set:Npn \\ {}
    #1
    \tabularnewline
    % 10 --> 0
    \int_set:Nn \l_tmpa_int { \int_mod:nn {\tl_count:N \l_tmpa_tl} {10} }%<-- modified
    \int_use:N \l_tmpa_int
    %%%%%%%%%% added   To spot errors
    \int_compare:nNnF
      {\l_tmpa_int} = {\tl_range:Nnn { \l_pi_decimales_tl } { \l_pi_posdecimales_int } { \l_pi_posdecimales_int }}
      {\textcolor{red}{?}}
    \int_gincr:N \l_pi_posdecimales_int
%%%%%%%%%%%%%%%%%%  
  \end{tabular}
    \exp_args:Ne \str_if_eq:nnTF { \tl_range:nnn { #1 } { -1 } { -1 } } 
    { \\ }  { \\ } { #2 }%<-- modified
  %  }
}

%%%%%%%%%%%%%%%%%%%%%% Added
\cs_new_protected:Npn \__damitr_pi_apostrophe:n #1
{
  \bool_if:NTF \l_pi_apostophe_bool 
  {
    \regex_match:nnTF { ' } { #1 } 
      { 
          \seq_set_split:Nnn \l_tmpb_seq {'} { #1 }
          \__damitr_pi_word:nn {\seq_item:Nn \l_tmpb_seq  {1}}{'}
          \__damitr_pi_word:nn {\seq_item:Nn \l_tmpb_seq  {2}}{\ }
      } 
      { 
          \__damitr_pi_word:nn{ #1 }{ \ }
      }
    }
    {\__damitr_pi_word:nn{ #1 }{ \ }}
}
\ExplSyntaxOff

\begin{document}

\lipsum[1][1-4]
\mnemonic{May I have a large container of coffee?}
\lipsum[2][1-4]

\mnemonic[verse]{ay I wish I could calculate pi,\\
"Eureka!" cried the great inventor,\\
Christmas pudding, Christmas pie,\\
Is the problem's very center.}
\lipsum[3][1-4]

% Maurice Decerf
\selectlanguage{french}
\apostrophe{1}
\mnemonic[verse]{Que j'aime à faire apprendre un nombre utile aux sages!\\
Glorieux Archimède, artiste ingénieux,\\
Toi de qui Syracuse aime encore la gloire,\\
Soit ton nom conservé par de savants grimoires!\\
Jadis, mystérieux, un problème bloquait\\
Tout l'admirable procédé, l'œuvre grandiose\\
Que Pythagore découvrit aux anciens Grecs.\\
Ô, quadrature! Vieux tourment du philosophe!\\
Insoluble rondeur, trop longtemps vous avez\\
Défié Pythagore et ses imitateurs.\\
Comment intégrer l'espace bien circulaire?\\
Former un triangle auquel il équivaudra?\\
Nouvelle invention: Archimède inscrira\\
Dedans un hexagone, appréciera son aire,\\
Fonction du rayon. Pas trop ne s'y tiendra\\
Dédoublera chaque élément antérieur;\\
Toujours de l'orbe calculée approchera;\\
Définira limite; enfin, l'arc, le limiteur\\
De cet inquiétant cercle, ennemi trop rebelle!\\
Professeur, enseignez son problème avec zèle!}
\lipsum[3][1-4]
\end{document}

enter image description here

And the French mnemonic from Maurice Decerf:

French mnemonic

You must log in to answer this question.

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