3

Here is my problem:

I often copy text from a PDF (let's say one that is nicely formatted or scanned cleanly) and like to post it in an active org-mode file I have for academic research. However, pasting it with C-y typically inherits a lot of the new lines that PDF text tends to carry, requiring me to fix the text manually.

I've checked around for a solution to this problem, but the closest I found was a previous question on overflow, but it only works partially. It requires that I select the newly pasted text with C-x C-x, call replace-string with C-q C-j RET RET.

This typically removes most of the new lines effectively, but will often eliminate the space between two words that were on the end of the previous line and the first on the next (example would be 'betweentwo' in this sentence).

Any common fix to this? If possible, I would like a general solution that allows me to paste text from any source as simply plain text. I'm using Linux and emacs 24.5 for clarification. I don't think any of my own configurations are in conflict, but if my init file is wanted, I can post that on request.

EDIT: Pictures to show the problem.

Here's sample text of a pdf I want to copy. It's either a direct PDF export or a very clean scan, so I have no problem copying text accurately to my clipboard (No incorrect words, symbols, etc).

Sample Text

Here's what it looks like pasted C-y in emacs (org-mode on). Notice how it doesn't just let the lines wrap naturally. This is actually a cleaner yank than most, but the problem is still present.

enter image description here

Here's what it looks like after I use the make-shift method I linked in the original post. As of now, I'm re-highlight the region with C-x C-x and then use ispell-region to methodically correct the words combined together.

enter image description here

Solution: YoungFrog's answer below clarified my problem. TL;DR, M-q, fill-column, does well to reformat the text I yank from PDFs into paragraphs of length determined by emacs' fill-column (70 by default).

In addition, I was using visual-line-mode, which can make fill-column look like it's not working right since the minor-mode is intended to visually wrap text with "soft new lines", as YoungFrog also pointed out.

A useful package that combines visual-line-mode with the formatting benefits of fill-column is visual-fill-column, available on MELPA.

12
  • I suspect this has less to do with Emacs and more how text is represented in a PDF (spoiler: messy) document. Not too surprising if you consider that it's essentially a print preview format, so as long as the document looks right, how its contents are represented is less of a concern...
    – wasamasa
    Apr 16, 2016 at 19:21
  • I totally agree. I used to use Microsoft Word extensively for public policy related work and had to deal with it. That said, I was able to find macros for MSWord that easily fixed the problem with a single function key press. Hopefully someone has a similar configuration and will see this.
    – zaile
    Apr 16, 2016 at 20:21
  • In principle, does this 2-step process work: 1., replace the new lines with spaces, 2. fill-region on the selected text?
    – Dan
    Apr 16, 2016 at 21:40
  • 1
    Does M-q not work good enough? If so please give examples of text.
    – YoungFrog
    Apr 17, 2016 at 4:21
  • 1
    Indeed it's related to visual-line-mode : M-q does not rely on visual line mode and instead adds hard newlines in your text to wrap before reaching fill-column characters on each line. OTOH, visual line mode arranges to visually wrap text at the right margin, so that your text seems to have a newline, but in fact it doesn't. This is sometimes called "soft newline".
    – YoungFrog
    Apr 17, 2016 at 20:39

1 Answer 1

3

You're looking for M-q (which runs the command fill-paragraph) : it will refill the paragraph according to the current value of fill-column by removing/adding hard newlines at the right spots for you. You can use C-x f to change the value of fill-column.

If you prefer to avoid hard newlines at all (e.g. because you rely on visual-line-mode and word-wrap), you can set fill-column to a ridiculously large value (like 99999), or let unfill-paragraph or unfill-region do that for you. Those functions are available on emacswiki as you already noticed in a comment :

1
  • 1
    Thank you very much clarifying those issues for me. I don't know how all the rules work on stackoverflow yet, but in addition to your advise/solution, I also found a MELPA package called visual-line-column-mode that makes visual-line-mode wrap at fill-column when reached AND at the edge of the window. Together, I still get the visual benefit of visual-line-mode while also maintaining a healthy column length/formatting.
    – zaile
    Apr 17, 2016 at 21:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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