emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: "Juan Manuel Macías" <maciaschain@posteo.net>
To: Ihor Radchenko <yantar92@posteo.net>
Cc: orgmode <emacs-orgmode@gnu.org>
Subject: Re: [patch] ox-latex.el: fix blank lines behavior in verse block
Date: Fri, 11 Aug 2023 18:52:46 +0000	[thread overview]
Message-ID: <87ttt59zsx.fsf@posteo.net> (raw)
In-Reply-To: <874jl5526t.fsf@localhost> (Ihor Radchenko's message of "Fri, 11 Aug 2023 10:00:10 +0000")

[-- Attachment #1: Type: text/plain, Size: 1647 bytes --]

Ihor Radchenko writes:

> Juan Manuel Macías <maciaschain@posteo.net> writes:
>
>> ... In any case, the fact that the verse block can also be used to
>> literally export line breaks and horizontal/vertical spaces is
>> interesting. Something occurred to me that I don't know if it's a bit
>> foolhardy: how about a LaTeX attribute ':literal t'? If used, it would
>> not export to a verse environment (specialized in poetry) but to normal
>> text, without environment, but with line breaks and horizontal and
>> vertical spacing preserved... wdyt?
>
> If one uses verse blocks to export to multiple backends, your suggestion
> sounds reasonable. That way, export results will look closer across
> different export backends.

How about this (pre-)patch? With the `:literal' attr., the content of the
block is exported "as is", with all manual formatting preserved.

I have made some modifications in the horizontal and vertical spaces (in
case :literal is used), because the em does not seem the correct length
to me. The em equals the font size in points, and, vertically, it would
not equal a blank line (line spacing is usually 120% of the em), so it's
safest to use \baselineskip. Thus, \vspace*{\baselineskip} is identical
to leaving an empty line. As for the horizontal space, the em is greater
than the normal space. The closest thing would be to use
\fontdimen2\font. So, if one puts 6 manual spaces (with the spacebar
key), it is exported as \hspace*{6\fontdimen2\font}

-- 
Juan Manuel Macías

https://juanmanuelmacias.com

https://lunotipia.juanmanuelmacias.com

https://gnutas.juanmanuelmacias.com

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-lisp-ox-latex.el-add-the-literal-attribute-to-verse-.patch --]
[-- Type: text/x-patch, Size: 3962 bytes --]

From baf9cc50313bb7df94e8173349db9c834f1ccf64 Mon Sep 17 00:00:00 2001
From: Juan Manuel Macias <maciaschain@posteo.net>
Date: Fri, 11 Aug 2023 19:57:49 +0200
Subject: [PATCH] lisp/ox-latex.el: add the `:literal' attribute to verse
 block.

* (org-latex-verse-block): now the treatment of blank lines is
consistent with the syntax of the LaTeX `verse' environment, and the
one provided by the `verse' package. If the `':literal attribute is
used, the content is not exported within a `verse' environment, but as-is,
preserving horizontal spaces, line breaks, and blank lines.
---
 lisp/ox-latex.el | 52 ++++++++++++++++++++++++++++++++++++------------
 1 file changed, 39 insertions(+), 13 deletions(-)

diff --git a/lisp/ox-latex.el b/lisp/ox-latex.el
index 31cad1dc4..557ceee1b 100644
--- a/lisp/ox-latex.el
+++ b/lisp/ox-latex.el
@@ -4116,32 +4116,58 @@ contextual information."
   (let* ((lin (org-export-read-attribute :attr_latex verse-block :lines))
          (latcode (org-export-read-attribute :attr_latex verse-block :latexcode))
          (cent (org-export-read-attribute :attr_latex verse-block :center))
-         (attr (concat
-	        (if cent "[\\versewidth]" "")
-	        (if lin (format "\n\\poemlines{%s}" lin) "")
-	        (if latcode (format "\n%s" latcode) "")))
+         (lit (org-export-read-attribute :attr_latex verse-block :literal))
+         (attr (if (not lit)
+		   (concat
+		    (if cent "[\\versewidth]" "")
+		    (if lin (format "\n\\poemlines{%s}" lin) "")
+		    (if latcode (format "\n%s" latcode) ""))
+		 ""))
          (versewidth (org-export-read-attribute :attr_latex verse-block :versewidth))
-         (vwidth (if versewidth (format "\\settowidth{\\versewidth}{%s}\n" versewidth) ""))
-         (linreset (if lin "\n\\poemlines{0}" "")))
+         (vwidth (if (not lit)
+		     (if versewidth (format "\\settowidth{\\versewidth}{%s}\n" versewidth) "")
+		   ""))
+         (linreset (if (not lit)
+		       (if lin "\n\\poemlines{0}" "")
+		     "")))
     (concat
      (org-latex--wrap-label
       verse-block
       ;; In a verse environment, add a line break to each newline
       ;; character and change each white space at beginning of a line
-      ;; into a space of 1 em.  Also change each blank line with
-      ;; a vertical space of 1 em.
-      (format "%s\\begin{verse}%s\n%s\\end{verse}%s"
+      ;; into a normal space, calculated with `\fontdimen2\font'.
+      ;; One or more blank lines between lines are exported as a
+      ;; single blank line.  If the `:literal' attribute is used,
+      ;; CONTENTS is exported as is, with no environment, preserving
+      ;; line breaks and vertical and horizontal spaces.
+      (format (if (not lit)
+		  "%s\\begin{verse}%s\n%s\\end{verse}%s"
+		"%s%s\n%s%s")
 	      vwidth
 	      attr
 	      (replace-regexp-in-string
-	       "^[ \t]+" (lambda (m) (format "\\hspace*{%dem}" (length m)))
+	       "^[ \t]+" (lambda (m) (format "\\hspace*{%d\\fontdimen2\\font}" (length m)))
 	       (replace-regexp-in-string
-                (concat "^[ \t]*" (regexp-quote org-latex-line-break-safe) "$")
-	        "\\vspace*{1em}"
+                (if (not lit)
+		    (concat "\\("
+			    (regexp-quote org-latex-line-break-safe)
+			    "\n\\)"
+			    "\\(^[ \t]*"
+			    (regexp-quote org-latex-line-break-safe)
+			    "\n"
+			    "\\)+")
+		  (concat "^[ \t]*" (regexp-quote org-latex-line-break-safe) "$"))
+	        (if (not lit)
+		    (if lin "\\\\!\n\n" "\n\n")
+		  "\\vspace*{\\baselineskip}")
 	        (replace-regexp-in-string
 	         "\\([ \t]*\\\\\\\\\\)?[ \t]*\n"
                  (concat org-latex-line-break-safe "\n")
-	         contents nil t)
+	         (if (not lit)
+                     ;; Remove any blank lines before and after CONTENTS.
+                     (concat (org-trim contents t) "\n")
+		   contents)
+                 nil t)
                 nil t)
                nil t)
               linreset)
-- 
2.41.0


  reply	other threads:[~2023-08-11 18:53 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-08-06 12:03 [patch] ox-latex.el: fix blank lines behavior in verse block Juan Manuel Macías
2023-08-07 11:40 ` Ihor Radchenko
2023-08-07 17:23   ` Juan Manuel Macías
2023-08-09  7:57     ` Ihor Radchenko
2023-08-09  8:41       ` Juan Manuel Macías
2023-08-10  9:27         ` Ihor Radchenko
2023-08-10 10:39           ` Juan Manuel Macías
2023-08-11 10:00             ` Ihor Radchenko
2023-08-11 18:52               ` Juan Manuel Macías [this message]
2023-08-12  7:56                 ` Ihor Radchenko
2023-08-12 11:28                   ` Juan Manuel Macías
2023-08-13  8:06                     ` Ihor Radchenko
2023-08-14 20:10                   ` Juan Manuel Macías
2023-08-15 10:08                     ` Ihor Radchenko
2023-08-15 11:50                       ` Juan Manuel Macías
2023-08-15 11:53                         ` Ihor Radchenko
2023-08-15 14:25                           ` Juan Manuel Macías
2023-08-16  8:10                             ` Ihor Radchenko
2023-08-16 14:10                               ` Juan Manuel Macías
2023-08-17 10:35                                 ` Ihor Radchenko
2023-08-17 20:17                                   ` Juan Manuel Macías
2023-08-18  8:44                                     ` Ihor Radchenko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87ttt59zsx.fsf@posteo.net \
    --to=maciaschain@posteo.net \
    --cc=emacs-orgmode@gnu.org \
    --cc=yantar92@posteo.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).