From: "András Simonyi" <andras.simonyi@gmail.com>
To: emacs-orgmode list <emacs-orgmode@gnu.org>
Subject: [PATCH] oc-csl: Improve LaTeX bibliography formatting
Date: Sun, 6 Nov 2022 10:42:22 +0100 [thread overview]
Message-ID: <CAOWRwxDs85nj=Si-ERyYK35OpePtsLPevNKWWJdU5-i42oLetQ@mail.gmail.com> (raw)
[-- Attachment #1: Type: text/plain, Size: 658 bytes --]
Dear All,
the attached patch substantially improves the formatting of CSL-based
bibliographies in LaTeX export by supporting in-style formatting
settings that were previously ignored, most importantly,
'second-field-align', which is typically used for numeric styles such
as ieee.csl.
I'm sending this while another oc-csl patch (about affix and locator
formatting) is still under discussion because I consider it a priority
for the 9.6 release. (Not supporting those formatting settings
rendered a large number of styles, some of them pretty popular, close
to being unusable.)
Thanks in advance for your feedback!
best wishes,
András
[-- Attachment #2: 0001-oc-csl-Improve-LaTeX-bibliography-formatting.patch --]
[-- Type: text/x-patch, Size: 6831 bytes --]
From bc92683a6326e7f97093b401caf453a76b76ba46 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andr=C3=A1s=20Simonyi?= <andras.simonyi@gmail.com>
Date: Sun, 2 Oct 2022 19:20:36 +0200
Subject: [PATCH] oc-csl: Improve LaTeX bibliography formatting
* lisp/oc-csl.el (org-cite-csl--output-format): Use the dedicated
'org-latex' citeproc formatter to export references in LaTeX.
(org-cite-csl-latex-preamble, org-cite-csl--generate-latex-preamble,
org-cite-csl-finalizer): Insert a preamble fragment compatible with
the 'org-latex' citeproc formatter.
(org-cite-csl-latex-label-separator,
org-cite-csl-latex-label-width-per-char): Introduce additional
variables to control bibliography formatting.
---
lisp/oc-csl.el | 98 ++++++++++++++++++++++++++++++++++++++++++--------
1 file changed, 83 insertions(+), 15 deletions(-)
diff --git a/lisp/oc-csl.el b/lisp/oc-csl.el
index 1ccb74e92..1807efaea 100644
--- a/lisp/oc-csl.el
+++ b/lisp/oc-csl.el
@@ -214,6 +214,68 @@ Used only when `second-field-align' is activated by the used CSL style."
:type 'string
:safe #'stringp)
+(defcustom org-cite-csl-latex-label-separator "0.6em"
+ "Distance between citation label and bibliography item for LaTeX
+output in valid LaTeX units. Used only when `second-field-align'
+is activated by the used CSL style."
+ :group 'org-cite
+ :package-version '(Org . "9.6")
+ :type 'string
+ :safe #'stringp)
+
+(defcustom org-cite-csl-latex-label-width-per-char "0.45em"
+ "Character width in LaTeX units for calculating entry label widths.
+Used only when `second-field-align' is activated by the used CSL
+style."
+ :group 'org-cite
+ :package-version '(Org . "9.6")
+ :type 'string
+ :safe #'stringp)
+
+;; The following was inspired by and mostly follows how Pandoc's
+;; (<https://github.com/jgm/pandoc>) default LaTeX template handles
+;; CSL output. Many thanks to the author, John MacFarlane!
+(defcustom org-cite-csl-latex-preamble
+ "\\usepackage{calc}
+\\newlength{\\cslhangindent}
+\\setlength{\\cslhangindent}{[CSL-HANGINDENT]}
+\\newlength{\\csllabelsep}
+\\setlength{\\csllabelsep}{[CSL-LABELSEP]}
+\\newlength{\\csllabelwidth}
+\\setlength{\\csllabelwidth}{[CSL-LABELWIDTH-PER-CHAR] * [CSL-MAXLABEL-CHARS]}
+\\newenvironment{citeprocbib}[2] % 1st arg. is hanging-indent, 2nd entry spacing.
+ {% By default, paragraphs are not indented.
+ \\setlength{\\parindent}{0pt}
+ % Hanging indent is turned on when first argument is 1.
+ \\ifodd #1
+ \\let\\oldpar\\par
+ \\def\\par{\\hangindent=\\cslhangindent\\oldpar}
+ \\fi
+ % Set entry spacing based on the second argument.
+ \\setlength{\\parskip}{\\parskip + #2\\baselineskip}
+ }%
+ {}
+\\newcommand{\\cslblock}[1]{#1\\hfill\\break}
+\\newcommand{\\cslleftmargin}[1]{\\parbox[t]{\\csllabelsep + \\csllabelwidth}{#1}}
+\\newcommand{\\cslrightinline}[1]
+ {\\parbox[t]{\\linewidth - \\csllabelsep - \\csllabelwidth}{#1}\\break}
+\\newcommand{\\cslindent}[1]{\\hspace{\\cslhangindent}#1}
+\\makeatletter
+\\newcommand{\\citeprocitem}[2]
+ {\\protect\\hyper@linkstart{cite}{citeproc_bib_item_#1}#2\\hyper@linkend}
+\\makeatother"
+ "LaTeX preamble content inserted by the `csl' citation processor.
+
+The placeholders [CSL-HANGINDENT], [CSL-LABELSEP],
+[CSL-LABELWIDTH-PER-CHAR] and [CSL-MAXLABEL-CHARS] are replaced,
+respectively, by the contents of the customizable variables
+`org-cite-csl-latex-hanging-indent', `org-cite-csl-latex-label-separator',
+`org-cite-csl-latex-label-width-per-char', and the maximal label length
+in the bibliography measured in characters."
+ :group 'org-cite
+ :type 'string
+ :package-version '(Org . "9.6"))
+
\f
;;; Internal variables
(defconst org-cite-csl--etc-dir
@@ -413,7 +475,7 @@ corresponding to one of the output formats supported by Citeproc: `html',
(let ((backend (plist-get info :back-end)))
(cond
((org-export-derived-backend-p backend 'html) 'html)
- ((org-export-derived-backend-p backend 'latex) 'latex)
+ ((org-export-derived-backend-p backend 'latex) 'org-latex)
(t 'org))))
(defun org-cite-csl--style-file (info)
@@ -670,6 +732,21 @@ value is the bibliography as rendered by Citeproc."
(plist-put info :cite-citeproc-rendered-bibliographies result)
result)))))
+(defun org-cite-csl--generate-latex-preamble (info)
+ "Generate the CSL-related part of the LaTeX preamble.
+INFO is the export state, as a property list."
+ (let* ((parameters (cadr (org-cite-csl--rendered-bibliographies info)))
+ (max-offset (cdr (assq 'max-offset parameters)))
+ (result org-cite-csl-latex-preamble))
+ (map-do (lambda (placeholder replacement)
+ (when (string-match placeholder result)
+ (setq result (replace-match replacement t t result))))
+ `("\\[CSL-HANGINDENT\\]" ,org-cite-csl-latex-hanging-indent
+ "\\[CSL-LABELSEP\\]" ,org-cite-csl-latex-label-separator
+ "\\[CSL-LABELWIDTH-PER-CHAR\\]" ,org-cite-csl-latex-label-width-per-char
+ "\\[CSL-MAXLABEL-CHARS\\]" ,(number-to-string max-offset)))
+ result))
+
\f
;;; Export capability
(defun org-cite-csl-render-citation (citation _style _backend info)
@@ -714,12 +791,7 @@ INFO is the export state, as a property list."
org-cite-csl-html-hanging-indent
org-cite-csl-html-hanging-indent))
output))
- ('latex
- (if (cdr (assq 'hanging-indent parameters))
- (format "\\begin{hangparas}{%s}{1}\n%s\n\\end{hangparas}"
- org-cite-csl-latex-hanging-indent
- output)
- output))
+ ('org-latex output)
(_
;; Parse Org output to re-export it during the regular export
;; process.
@@ -730,18 +802,14 @@ INFO is the export state, as a property list."
OUTPUT is the export document, as a string. INFO is the export state, as a
property list."
(org-cite-csl--barf-without-citeproc)
- (if (not (eq 'latex (org-cite-csl--output-format info)))
+ (if (not (eq 'org-latex (org-cite-csl--output-format info)))
output
(with-temp-buffer
(save-excursion (insert output))
(when (search-forward "\\begin{document}" nil t)
- (goto-char (match-beginning 0))
- ;; Ensure that \citeprocitem is defined for citeproc-el.
- (insert "\\makeatletter\n\\newcommand{\\citeprocitem}[2]{\\hyper@linkstart{cite}{citeproc_bib_item_#1}#2\\hyper@linkend}\n\\makeatother\n\n")
- ;; Ensure there is a \usepackage{hanging} somewhere or add one.
- (let ((re (rx "\\usepackage" (opt "[" (*? nonl) "]") "{hanging}")))
- (unless (re-search-backward re nil t)
- (insert "\\usepackage[notquote]{hanging}\n"))))
+ (goto-char (match-beginning 0))
+ ;; Insert the CSL-specific parts of the LaTeX preamble.
+ (insert (org-cite-csl--generate-latex-preamble info)))
(buffer-string))))
\f
--
2.25.1
next reply other threads:[~2022-11-06 9:43 UTC|newest]
Thread overview: 18+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-11-06 9:42 András Simonyi [this message]
2022-11-07 2:47 ` [PATCH] oc-csl: Improve LaTeX bibliography formatting Ihor Radchenko
2022-11-07 11:15 ` András Simonyi
2022-11-08 5:26 ` Ihor Radchenko
2022-12-11 19:00 ` András Simonyi
2022-12-12 9:07 ` Ihor Radchenko
2022-12-12 11:15 ` András Simonyi
2022-12-12 11:24 ` Ihor Radchenko
2022-12-29 15:40 ` Bastien Guerry
2022-12-31 12:22 ` Screenshots in ORG-NEWS (was: [PATCH] oc-csl: Improve LaTeX bibliography formatting) Ihor Radchenko
2023-01-02 15:11 ` Screenshots in ORG-NEWS Bastien
2023-01-03 10:34 ` Ihor Radchenko
2023-01-03 12:36 ` Bastien
2022-12-13 16:05 ` [PATCH] oc-csl: Improve LaTeX bibliography formatting Timothy
2022-12-13 19:03 ` András Simonyi
2022-12-27 22:32 ` András Simonyi
2022-12-29 10:14 ` Ihor Radchenko
2022-12-29 21:51 ` András Simonyi
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='CAOWRwxDs85nj=Si-ERyYK35OpePtsLPevNKWWJdU5-i42oLetQ@mail.gmail.com' \
--to=andras.simonyi@gmail.com \
--cc=emacs-orgmode@gnu.org \
/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).