2

I am trying to adapt this answer to produce the same result with a verse environment: I want to be able to copy a poem out of a PDF maintaining its indentation (numbering included) as typeset in the PDF.

Although the following MWE produces the desired result with the text in the verbatim environment, I can't get it working also with the verse environment that is additionally complicated by the verse numbering (which I also what to copy out from the PDF).

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{verse}
\usepackage{color}
\input{glyphtounicode}
\pdfglyphtounicode{visiblespace}{A0}
\pdfglyphtounicode{blank}{A0}
\pdfglyphtounicode{visualspace}{A0}
\pdfglyphtounicode{uni2423}{A0}
\pdfgentounicode=1
\begin{document}\showoutput
\makeatletter
\def\@xobeysp{\textcolor{white}{\char32}}
\makeatother

% Set style for poem typesetting
\verselinenumbersleft
\setlength{\vrightskip}{-2em}
\poemlines{5}

\begin{verbatim}
S'i' fosse foco

S'i' fosse foco, arderei 'l mondo;
  s'i' fosse vento, lo tempesterei;
  s'i' fosse acqua, i' l'annegherei;
  s'i' fosse Dio, mandereil' en profondo;

s'i' fosse Papa, allor sarei giocondo,
  che` tutti cristiani 'mbrigarei;
  s'i' fosse 'mperator, ben lo farei:
  a tutti taglierei lo capo a tondo.

S'i' fosse morte, andarei a mi' padre;
  s'i' fosse vita, non starei con lui:
  similmente faria con mi' madre.

S'i' fosse Cecco, com'i' sono e fui,
  torrei le donne giovani e leggiadre:
  le zoppe e vecchie lasserei altrui. 
\end{verbatim}

\begin{verse}
\poemtitle{S'i' fosse foco}

S'i' fosse foco, arderei 'l mondo;\\
\hspace{2em}s'i' fosse vento, lo tempesterei;\\
\hspace{2em}s'i' fosse acqua, i' l'annegherei;\\
\hspace{2em}s'i' fosse Dio, mandereil' en profondo;\\!
s'i' fosse Papa, allor sarei giocondo,\\
\hspace{2em}che` tutti cristiani 'mbrigarei;\\
\hspace{2em}s'i' fosse 'mperator, ben lo farei:\\
\hspace{2em}a tutti taglierei lo capo a tondo.\\!
S'i' fosse morte, andarei a mi' padre;\\
\hspace{2em}s'i' fosse vita, non starei con lui:\\
\hspace{2em}similmente faria con mi' madre.\\!
S'i' fosse Cecco, com'i' sono e fui,\\
\hspace{2em}torrei le donne giovani e leggiadre:\\
\hspace{2em}le zoppe e vecchie lasserei altrui.\\!
\end{verse}
\end{document}

Indeed I have no idea what the code

\input{glyphtounicode}
\pdfglyphtounicode{visiblespace}{A0}
\pdfglyphtounicode{blank}{A0}
\pdfglyphtounicode{visualspace}{A0}
\pdfglyphtounicode{uni2423}{A0}
\pdfgentounicode=1
\begin{document}\showoutput
\makeatletter
\def\@xobeysp{\textcolor{white}{\char32}}
\makeatother

produces, so I can't really tweak it for the verse environment.

Francesco
  • 4,624
  • 4
  • 35
  • 66
  • Which features of verse do you need? because as it stands I am afraid rather extensive re-write of verse innards are needed; things like \hspace will need to be redefined inside the environment. If mainly you need the numbering, I would go for an environment simply obeying spaces and line breaks and adding the automatic numbering. –  May 15 '14 at 06:35
  • Could you add the information about your os and pdf viewer: because the (verbatim) code you provide (using the answer you linked too) does not preserve the indentation on copy-paste with the versions of Acrobat Reader I have tried on Linux and Mac OS X. –  May 15 '14 at 14:36
  • In my testing, to use the code in my answer with evince reader on linux, one needs to uncomment the \pdfglyphtounicode lines. On the other hand the code as is works well with acroread, but ceases to work with it if the \pdfglyphtounicode lines are used. –  May 15 '14 at 16:23

1 Answers1

2

Here is an approach which defines a homemade verse environment with line count, where horizontal spaces from the source are obeyed and vertical spaces have to be keyed in using for example \medskip.

The copy-paste thing is viewer dependent, with acroread 9 on a Fedora 13 as I just tested it works.

poem

and copy paste from acroread 9 gives:

                     S’i’ fosse foco
          S’i’ fosse foco, arderei ’l mondo;
            s’i’ fosse vento, lo tempesterei;
            s’i’ fosse acqua, i’ l’annegherei;
            s’i’ fosse Dio, mandereil’ en profondo;
5         s’i’ fosse Papa, allor sarei giocondo,
            che‘ tutti cristiani ’mbrigarei;
            s’i’ fosse ’mperator, ben lo farei:
            a tutti taglierei lo capo a tondo.
          S’i’ fosse morte, andarei a mi’ padre;
10          s’i’ fosse vita, non starei con lui:
            similmente faria con mi’ madre.
          S’i’ fosse Cecco, com’i’ sono e fui,
            torrei le donne giovani e leggiadre:
            le zoppe e vecchie lasserei altrui.
And normal text

The code has two customizable commands \myverseleftspaces for the number of left spaces, where the line numbers are typeset, and \myversemodulo which is here 5, only multiples of this are displayed as line numbers. The title must be on one line. This is not a verbatim environment, LaTeX commands may be used. The spaces on the left are from the monotype font family, the inter-word spaces in the poem from the font used for the poem.

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{color}

% the following is NOT needed if acroread 9 on Fedora Linux 13
% to the contrary it works better without
% \pdfglyphtounicode{visiblespace}{A0}
% \pdfglyphtounicode{blank}{A0}
% \pdfglyphtounicode{visualspace}{A0}
% \pdfglyphtounicode{uni2423}{A0}
% \pdfgentounicode=1


\newcounter{myversecount}
\newlength{\myversespwidth}

\newsavebox{\myversespbox}

\newcommand{\myverseresetspbox}[1][ ]{%
    \settowidth{\myversespwidth}{#1}%
    \sbox{\myversespbox}{\makebox[\myversespwidth]{\textcolor{white}{\char32}}}%
}

\newcommand{\myversesp}[1]{\ifnum \numexpr#1>0
             \usebox{\myversespbox}%
             \expandafter\myversesp\expandafter{\the\numexpr#1-1}\fi }

% \newlength{\myverseleftmargin}
% \setlength{\myverseleftmargin}{10ex}

\newcommand{\myverseleftspaces}{10}% at least 3

\newcommand{\myversemodulo}{5}

\newcommand{\myversestart}{\stepcounter{myversecount}%
   \texttt{\myverseresetspbox
   \ifnum \value{myversecount}=0
%              \makebox[\myverseleftmargin][l]{\myversesp{\myverseleftspaces}}%
               \myversesp{\myverseleftspaces}%
   \else
         \ifnum \numexpr\value{myversecount}-
                      \myversemodulo*(\value{myversecount}/\myversemodulo)=0
                \ifnum\value{myversecount}<10
                   %\makebox[\myverseleftmargin][l]
                     \arabic{myversecount}\myversesp{\myverseleftspaces-1}%
                \else
                   \ifnum\value{myversecount}<100
                    %\makebox[\myverseleftmargin][l]
                      \arabic{myversecount}\myversesp{\myverseleftspaces-2}%
                   \else
                     %\makebox[\myverseleftmargin][l]
                      \arabic{myversecount}\myversesp{\myverseleftspaces-3}%
                   \fi
                \fi
        \else
          %\makebox[\myverseleftmargin][l]{
           \myversesp{\myverseleftspaces}%
        \fi
   \fi }% end of texttt
}

\newenvironment{myverse}
   {\setcounter{myversecount}{-1}% title should be on one line only
    \myverseresetspbox
    \parindent 0pt
    \everypar{\myversestart}%
    \begingroup\lccode`~ 32
    \lowercase{\endgroup\def~{\usebox{\myversespbox}}}%
    \obeylines\obeyspaces}
   {}

\begin{document}

\begin{myverse}
           \textbf{S'i' fosse foco}
\medskip
S'i' fosse foco, arderei 'l mondo;
  s'i' fosse vento, lo tempesterei;
  s'i' fosse acqua, i' l'annegherei;
  s'i' fosse Dio, mandereil' en profondo;
\smallskip
s'i' fosse Papa, allor sarei giocondo,
  che` tutti cristiani 'mbrigarei;
  s'i' fosse 'mperator, ben lo farei:
  a tutti taglierei lo capo a tondo.
\smallskip
S'i' fosse morte, andarei a mi' padre;
  s'i' fosse vita, non starei con lui:
  similmente faria con mi' madre.
\smallskip
S'i' fosse Cecco, com'i' sono e fui,
  torrei le donne giovani e leggiadre:
  le zoppe e vecchie lasserei altrui. 
\end{myverse}
\noindent And normal text

\end{document}
  • if title occupies two lines, just replace in the code above \medskip by \medskip\setcounter{myversecount}{0}% –  May 15 '14 at 12:57
  • Testing on fedora 13 the above code: pasting (in emacs unicoded encoded buffer) from acroread gives the wished-for result, while pasting from both evince and okular uses the 0x2423 OPEN BOX character. –  May 15 '14 at 16:16
  • And if one uses \pdfglyphtounicode from the commented-out code, then pasting from okular uses the 0xA0 NO-BREAK SPACE, pasting from evince gives the wished-for result, and pasting from acroread has all spaces at the beginning of lines swallowed and consecutive spaces between words collapsed into one. –  May 15 '14 at 16:20