-
According to the
which works like Is there a way to get this behaviour to be supported with the \documentclass{article}
\usepackage[american]{babel}
\usepackage{csquotes}
\usepackage[style=apa]{biblatex}
\usepackage[citations]{markdown}
\begin{document}
\begin{markdown}
Here is a reference with parentheses: @someauthor [p. 233].
(Here is a reference without parentheses: @someauthor [p. 233].)
\end{markdown}
\printbibliography
\end{document} EDIT: I know that I can do EDIT: Note that it is probably best if parenthesis levels are counted for each paragraph rather than each line, so that the following works: (This is another reference to
@someauthor,
p. 233) It is probably also best if the parenthesis level counter is reset for every new paragraph so that a forgotten EDIT: It is probably also best if this can be solved in a way that doesn’t break existing LaTeX syntax involving parentheses when this appears in hybrid mode. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 14 replies
-
@sorsted This is related to the text citation renderer. The Markdown package provides a default renderer for LaTeX, which tries to cover a couple use cases (BibTeX, Natbib, and BibLaTeX), but you need to adopt it to special cases such as \usepackage{xpatch}
\xpatchcmd
\markdownLaTeXBibLaTeXTextCitations
{\textcites}%
{\nptextcites}%
{}%
{} Please note that default renderers are subject to change, so if you want to be sure that your code does not break in the long run (e.g. we might get rid of the |
Beta Was this translation helpful? Give feedback.
-
Update: The good guys at TeX.SX have provided me with the perfect solution which doesn’t even require us to break with the standard markdown citation syntax. Furthermore, this will work even if later we decide to instead use pandoc to convert the markdown document to LaTeX for further post-editing. @Witiko, please tell me if I did everything correctly and have the right amount of % Compile with LuaTeX:
\begin{filecontents*}{find_parens.lua}
-- From Marcel Krüger's answer at https://tex.stackexchange.com/a/654261/80496
-- How to translate boolean results to TeX macros selecting the right arguments
local bool_to_token = {[false] = token.create'use_ii:nn', [true] = token.create'use_i:nn'}
-- The codepoints we have to look for
local left_paren = string.byte'('
local right_paren = string.byte')'
-- Some boilerplate to define an unexpandable command sequence from Lua
local id = luatexbase.new_luafunction'ifInParensTF'
token.set_lua('ifInParensTF', id, 'protected')
lua.get_functions_table()[id] = function()
-- We want to look at the current list
local head = tex.nest.top.head
local level = 0
-- and look at all unprocessed characters
for _, char in node.traverse_char(head) do
-- Track the nesting level
if char == left_paren then
level = level + 1
elseif char == right_paren then
if level > 0 then
level = level - 1
else
-- Negative levels don't make sense, so we ignore the ) instead. Print a warning though to avoid surprises.
texio.write_nl'WARNING: Unable to reliably determine if inside parentheses since the paragraphs contains an unmatched right parentheses.'
texio.write_nl'The unmatched parentheses will be ignored.'
end
end
end
-- Finally insert a macro which will select the first or second argument dependingon whether level > 0
token.put_next(bool_to_token[level > 0])
end
\end{filecontents*}
\begin{filecontents*}{ref.bib}
@online{texSX,
author={{egreg et al.}},
year={2022},
title={{TeX Stack Exchange}},
url={https://tex.stackexchange.com/},
}
\end{filecontents*}
\documentclass{article}
\usepackage[american]{babel}
\usepackage{csquotes}
\usepackage[style=apa]{biblatex}
\directlua{require'find_parens'}
\addbibresource{ref.bib}
\usepackage[citations]{markdown}
\NewDocumentCommand \autotextcites {}{%
\ifInParensTF {%
\nptextcites
}{%
\textcites
}%
}
\NewDocumentCommand \autoparencites {}{%
\ifInParensTF {%
\nptextcites
}{%
\parencites
}%
}
\makeatletter
\let\markdownLaTeXCitationsCounter=\relax
\newcount\markdownLaTeXCitationsCounter
\def\markdownLaTeXBibLaTeXAPACitations#1#2#3#4#5{%
\advance\markdownLaTeXCitationsCounter by 1\relax
\ifnum\markdownLaTeXCitationsCounter>\markdownLaTeXCitationsTotal\relax
\autoparencites#1[#3][#4]{#5}%
\expandafter\@gobbletwo
\fi\markdownLaTeXBibLaTeXAPACitations{#1[#3][#4]{#5}}}
\def\markdownLaTeXBibLaTeXAPATextCitations#1#2#3#4#5{%
\advance\markdownLaTeXCitationsCounter by 1\relax
\ifnum\markdownLaTeXCitationsCounter>\markdownLaTeXCitationsTotal\relax
\autotextcites#1[#3][#4]{#5}%
\expandafter\@gobbletwo
\fi\markdownLaTeXBibLaTeXAPATextCitations{#1[#3][#4]{#5}}}
\markdownSetup{rendererPrototypes = {
cite = {%
\markdownLaTeXCitationsCounter=1%
\def\markdownLaTeXCitationsTotal{#1}%
\expandafter\markdownLaTeXBibLaTeXAPACitations
\expandafter{\expandafter}%
},
textCite = {%
\markdownLaTeXCitationsCounter=1%
\def\markdownLaTeXCitationsTotal{#1}%
\expandafter\markdownLaTeXBibLaTeXAPATextCitations
\expandafter{\expandafter}%
}%
}}
\makeatother
\begin{document}
\begin{markdown}
This follows from @texSX.
(It also follows from @texSX.)
(It also follows from [@texSX].)
(It also follows from
@texSX
among other places.)
(It also follows from other resources. Notice that we forgot a closing parenthesis.
But @texSX is a good start.
\end{markdown}
\printbibliography
\end{document} |
Beta Was this translation helpful? Give feedback.
Update: The good guys at TeX.SX have provided me with the perfect solution which doesn’t even require us to break with the standard markdown citation syntax. Furthermore, this will work even if later we decide to instead use pandoc to convert the markdown document to LaTeX for further post-editing. @Witiko, please tell me if I did everything correctly and have the right amount of
\expandafter
's. At least it compiles just fine.