7

Sometimes I need to send someone a .doc file of something I did using LaTeX. But when I copy+paste the PDF content generated by (La)TeX into Word the linebreaks are understood as paragraph characters (¶). I don't really mind that the .doc file is unformatted, but it's rather annoying to fix the text in Word every time, specially because the actual paragraph break is unknown...

Is there a way to replace the linebreaking character TeX uses so that when pasting into Word the text is "properly" pasted?

If the problem is not clear enough compile the following document:

\documentclass{article}
\usepackage{lipsum}
\begin{document}
\lipsum[0-5]
\end{document}

You should get LaTeX PDF output

Copy the PDF content and Paste to Word (if you have it), it results in: When pasted into Word...

There should be only five ¶ beacause there's only five paragraphs...

  • Exactly what are you using to copy the text from the PDF (which previewer?)? I'd assume that the copy just knows about lines, not paragraphs (basically verbatim line by line). And that is it is a mishap that word marks all line endings as a new paragraph. – daleif Aug 2 '18 at 13:34
  • 2
    For fairly simple documents, I have found that tools like pandoc or latex2rtf do a pretty good job of this. They operate on the LaTeX source file directly, so they don't rely on interpreting the PDF output. Sorry, no time to find links right now, but if you search for these names you can find some tips and examples of what they can do. – Paul Gessler Aug 2 '18 at 13:42
  • 2
    tex does not insert any character at a linebreak it just positions each line by coordinates, so any plain text cut and paste is a feature of the heuristics in your pdf reader not of tex. – David Carlisle Aug 2 '18 at 13:54
  • 1
    I don't see it as a workaround at all. The source document has much more information than the output PDF's selectable text stream (think of emphasis, bold text, font size changes, etc. that are lost when copying/pasting a selection of text from a PDF). Plus if you are copying multiple pages there are headings/page numbers to deal with and so on. – Paul Gessler Aug 2 '18 at 13:59
  • 2
    Try different PDF viewers. Skim and Preview on the Mac, for example, which use Apple's PDFkit, copy and paste with no trouble; Adobe Reader doesn't. – Alan Munn Aug 2 '18 at 15:20
12

You would need to tag the pdf if the reader should recognize units like "paragraphs". Without it it sees only lines.

As an example run the following with lualatex and compare with and without the activate-all key.

Attention!

tagpdf is an experimental package I wrote to investigate such questions. It is not really meant for production documents. It is not complete yet and things are bound to change. So if you use it you use it at your own risk.

%! needs lualatex for the best result
\documentclass{book}

\usepackage[ngerman]{babel} %to get some hyphenation
\prehyphenchar`\^^ad % with luatex improves copying of hyphenated words (if the font has the soft hyphen). 
\usepackage{tagpdf}
\tagpdfsetup{tabsorder=structure,
             activate-all, %deactivate to see the difference
             uncompress
             }
\usepackage{lipsum}%

\pagestyle{empty}

\begin{document}
\tagstructbegin{tag=Document}
  \tagstructbegin{tag=P}
   \tagmcbegin{tag=P}
    \lipsum[1]
   \tagmcend
  \tagstructend
\tagstructend   
\end{document} 

Result of copy and paste without and with activated tagpdf

enter image description here

  • Interesting... Then for multiple paragraphs one would need to tag the beginning and end of each paragraph? – Guilherme Zanotelli Aug 2 '18 at 14:27
  • Yes, you would have to do this - can be quite some work ;-). – Ulrike Fischer Aug 2 '18 at 14:30
  • 1
    Btw: an alternative to tagging would be to typeset your document with a very large line width. Then every paragraph is on its own line. – Ulrike Fischer Aug 2 '18 at 14:41
  • That's a good point... I just want to know one last thing: can we use pdflatex instead of lua? And can you make the example for two paragraphs? I don't really understand if I'm supposed to enclose each paragraph into both \tagstructure and \tagmc and also if I should repeat the value of ´tag` for every pararagph or use different values... Thank's – Guilherme Zanotelli Aug 2 '18 at 19:02
  • 1
    pdflatex is problematic as \tagmcbegin and \tagmcend must always be on the same (output) page with this engine, see the documentation for info about this. And yes you need the whole code around every paragraph, but you can naturally define shorter command names, E.g \PAR \EPAR. – Ulrike Fischer Aug 2 '18 at 19:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.