1

The question is general, but here's a specific case:

In LaTeX source files, it doesn't matter if two words are separated by one space character, multiple space characters, or even a linebreak - in the compiled document, the two words are just separated by one space. Many people will insert linebreaks to do manual line wrapping when writing LaTeX.

If I want to search a phrase containing multiple words inside the LaTeX source file, is Emacs able to disregard the white space ambiguities described above when searching?

4
  • 1
    Does (info "(emacs) Lax Search") answer your question?
    – Basil
    Commented Sep 5, 2021 at 9:34
  • @Basi Thanks for the link, I just need (setq search-whitespace-regexp "[[:space:]\n]+")
    – felix
    Commented Sep 5, 2021 at 9:58
  • Please either add that as an answer or delete the question. Comments can be deleted at any time. Q&A need to stand on their own (only they are searchable). Also: it's possible that this question is a duplicate - please search for that, and if so, delete it. Thx.
    – Drew
    Commented Sep 5, 2021 at 15:09
  • @phils thanks for the language advice, please feel free to suggest alternative wording. (Unfortunately I don't think I can edit the question anymore.)
    – felix
    Commented Sep 6, 2021 at 19:39

1 Answer 1

2

Following the link of @Basil, here's the needed ELisp code.

(setq search-whitespace-regexp "[[:space:]\n]+"))

To enable this whenever I edit LaTeX documents, I added the following code to my init file.

(defun my-latex-search-setting ()
  (setq search-whitespace-regexp "[[:space:]\n]+"))
(add-hook 'latex-mode-hook #'my-latex-search-setting)

Now if I press Ctrl-s to search for hello world, Emacs will match e.g.

hello    world

as well as

hello
world
2
  • 1
    search-whitespace-regexp is a global setting, so it doesn't make sense to set it in a mode hook. You should either set it globally or make it buffer local ((make-local-variable 'search-whitespace-regexp) in the mode hook before the assignment). Commented Sep 7, 2021 at 16:33
  • @Gilles'SO-stopbeingevil' Thanks, I've changed it to (defun my-latex-search-setting () (make-local-variable 'search-whitespace-regexp) (setq search-whitespace-regexp "[[:space:]\n]+"))
    – felix
    Commented Sep 8, 2021 at 18:39

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.