ConTeXt:将条件格式应用于参考书目条目

ConTeXt:将条件格式应用于参考书目条目

学术资助中一个相对常见的要求是提供参考书目,其中您的姓名或学生姓名的格式与其他作者姓名不同(例如以粗体文本或下划线显示)。有没有办法在 ConTeXt 中自动执行此操作?

不确定是否有用,但我以前曾使用过以下答案比布拉特克斯

我已经阅读了书目手册,我觉得通过将作者字段传递到 lua(并在那里进行一些文本处理),以下内容应该可以进行一些小的改动:

\startbuffer[ref]
@article{solo,
  author = {MyLast, MyFirst},
  title = {Solo work},
  journal = {Journal},
  year = {2000},
  month = {1},
  volume = {1}
}

@article{co,
  author = {MyLast, MyFirst and CoLast, CoFirst},
  title = {Joint work},
  journal = {Journal},
  month = {1},
  year = {2000},
  volume = {1}
}
\stopbuffer

\usebtxdataset[ref][ref.buffer]
\setupbtx[dataset=ref]

\definebtxrendering[ref][dataset=ref]
\setupbtx[default:cite]
  [alternative=authoryear,
   etallimit=1] 

\define[1]\formatname{
  % Placeholder formatting
  \ctxlua{tex.sprint('{\\bf ' .. '#1' .. '}')}
}

\starttexdefinition btx:test
   \formatname{\btxflush{author}}
\stoptexdefinition

\startsetups btx:default:list:article
    \texdefinition{btx:test}
    \texdefinition{btx:default:title}
    \texdefinition{btx:default:journal}
    \texdefinition{btx:default:year}
    \removeunwantedspaces
    \removepunctuation
    \btxperiod
\stopsetups

\starttext

Citations: \cite[solo], \cite[co]

\startsubject[title=Bibliography]
\placelistofpublications[ref][method=dataset]
\stopsection

\stoptext

但是\btxflush命令没有被解析,而且在生成首字母后,lua 看不到作者字段。我尝试了不同的扩展方法,但我仍然不清楚它们是如何工作的。

结果:

在此处输入图片描述

答案1

更新 2:(ConTeXt 邮件列表提供更多帮助)

基于此来自主题的回复

为了充分利用作者拆分和作者转换的内置方法,最好的方法是重新定义作者转换函数。因此,如果normalshort要使用转换,请将其定义为包含粗体术语的条件条目:

\startsetups btx:list:author:normalshort
    \fastsetup{btx:list:author:concat}
    \begingroup

    %%% New snippet
    \ctxluacode{document.BoldNames("\currentbtxdataset","\currentbtxtag",\number\currentbtxauthorindex)}
    %%%        

    \ifx\currentbtxinitials\empty \else
        \currentbtxinitials
        \btxparameter{separator:initials}
    \fi
    \ifx\currentbtxvons\empty \else
        \currentbtxvons
        \ifx\currentbtxsurnames\empty \else
            \btxparameter{separator:vons}
        \fi
    \fi
    \ifx\currentbtxsurnames\empty \else
        \currentbtxsurnames
        \ifx\currentbtxjuniors\empty \else
            \btxparameter{separator:juniors}
            \currentbtxjuniors
        \fi
    \fi
    \endgroup
    \fastsetup{btx:list:author:others}
\stopsetups 

可以找到其他作者转换定义publ-imp-作者.mkiv源代码。

下面的示例定义了document.BoldNames允许选择多个名称的函数(尽管对于更复杂的名称可能需要进行一些调整):

\startbuffer[ref]
@article{solo,
  author = {MyLast, MyFirst},
  title = {Solo work},
  journal = {Journal},
  year = {2000},
  month = {1},
  volume = {1}
}

@article{co,
  author = {OtherLast, OtherFirst and MyLast, MyFirst and CoLast, CoFirst},
  title = {Joint work},
  journal = {Journal},
  month = {1},
  year = {2000},
  volume = {1}
}
\stopbuffer

\usebtxdataset[ref][ref.buffer]
\setupbtx[dataset=ref]

\definebtxrendering[ref][dataset=ref]
\setupbtx[default:cite]
    [alternative=authoryear,
     etallimit=1,
     authorconversion=normalshort] 

\startluacode
    local BoldNames = {
        {firstnames = "MyFirst",
         surnames = "MyLast"},
        {initials = "C",
         surnames = "CoLast"},
    }

    function document.CompareNames(reference, targets)
        -- Loop over targets 
        for _, target in pairs(targets) do

            -- Loop over fields
            for key, value in pairs(target) do

                if reference[key] == nil then
                    break
                end

                local full_entry = ''
                for _, part in ipairs(reference[key]) do
                    full_entry = full_entry .. ' ' .. part
                end
                full_entry = string.sub(full_entry, 2)

                if full_entry == value then
                    return(true)
                end
            end     
        end
        return(false)
    end 

    function document.BoldNames(set, tag, aut)      
        local c = publications.getcasted(set, tag, "author")

        if document.CompareNames(c[aut], BoldNames) then
            context("\\bf")
        end
    end
\stopluacode


\startsetups btx:list:author:normalshort
    \fastsetup{btx:list:author:concat}
    \begingroup

    \ctxluacode{document.BoldNames("\currentbtxdataset","\currentbtxtag",\number\currentbtxauthorindex)}
    \ifx\currentbtxinitials\empty \else
        \currentbtxinitials
        \btxparameter{separator:initials}
    \fi
    \ifx\currentbtxvons\empty \else
        \currentbtxvons
        \ifx\currentbtxsurnames\empty \else
            \btxparameter{separator:vons}
        \fi
    \fi
    \ifx\currentbtxsurnames\empty \else
        \currentbtxsurnames
        \ifx\currentbtxjuniors\empty \else
            \btxparameter{separator:juniors}
            \currentbtxjuniors
        \fi
    \fi
    \endgroup
    \fastsetup{btx:list:author:others}
\stopsetups 



\starttext

Citations: \cite[solo] \cite[co]

\startsubject[title=Bibliography]
\placelistofpublications[ref][method=dataset]
\stopsection

\stoptext

结果:

在此处输入图片描述

更新:(在 ConTeXt 邮件列表的帮助下)

基于此来自主题的回复

使用publications.getcasted()函数,无需进行自定义拆分即可获得格式化的姓名。但是,仍然需要定义姓名输出的格式。以下代码使用了简单的 Lastname Initial. 示例格式(对于更复杂的姓名,可能会失败):

\startbuffer[ref]
@article{solo,
  author = {MyLast, MyFirst},
  title = {Solo work},
  journal = {Journal},
  year = {2000},
  month = {1},
  volume = {1}
}

@article{co,
  author = {MyLast, MyFirst and CoLast, CoFirst},
  title = {Joint work},
  journal = {Journal},
  month = {1},
  year = {2000},
  volume = {1}
}
\stopbuffer

\usebtxdataset[ref][ref.buffer]
\setupbtx[dataset=ref]

\definebtxrendering[ref][dataset=ref]
\setupbtx[default:cite]
  [alternative=authoryear,
   etallimit=1] 

\startluacode
    -- Define target name
    local target = { ['firstnames'] = 'MyFirst',
                     [ 'surnames' ] = 'MyLast'}


    function document.boldauthor(set,tag)
        c = publications.getcasted(set, tag, 'author')

        for i = 1,#c do
            local add_name = function() context.sprint(c[i].surnames[1] .. ' ' .. c[i].initials[1]) end

            if (c[i].surnames[1] == target.surnames) and (c[i].firstnames[1] == target.firstnames) then
                context.bold( add_name )
                context.sprint('., ')
            else
                add_name()
                context.sprint('., ')
            end
        end
    end
\stopluacode

\starttexdefinition btx:customauthor
    \ctxluacode{document.boldauthor("\currentbtxdataset","\currentbtxtag")}
\stoptexdefinition 

\startsetups btx:default:list:article
    \texdefinition{btx:customauthor}
    \texdefinition{btx:default:title}
    \texdefinition{btx:default:journal}
    \texdefinition{btx:default:year}
    \removeunwantedspaces
    \removepunctuation
    \btxperiod
\stopsetups

\starttext

Citations: \cite[solo] \cite[co]

\startsubject[title=Bibliography]
\placelistofpublications[ref][method=dataset]
\stopsection

\stoptext

结果:

在此处输入图片描述

旧答案:

我找到了一种颠覆该问题的方法,即将新条目键定义myauthor为的副本author(以避免影响引用),然后使用\btxdirect而不是btxflush。但是,此副本是在任何格式化之前生成的,这意味着该方法需要手动拆分和格式化名称(以生成首字母等),这不是很通用。

\startbuffer[ref]
@article{solo,
  author = {MyLast, MyFirst},
  title = {Solo work},
  journal = {Journal},
  year = {2000},
  month = {1},
  volume = {1}
}

@article{co,
  author = {MyLast, MyFirst and CoLast, CoFirst},
  title = {Joint work},
  journal = {Journal},
  month = {1},
  year = {2000},
  volume = {1}
}
\stopbuffer

\usebtxdataset[ref][ref.buffer]
\setupbtx[dataset=ref]

\definebtxrendering[ref][dataset=ref]
\setupbtx[default:cite]
  [alternative=authoryear,
   etallimit=1] 

\startluacode
  local dataset = publications.datasets.ref
  for tag, entry in pairs(dataset.luadata) do
    -- Do complex formatting in lua... the following is a placeholder
    dataset.luadata[tag].myauthor = '{\\bf ' .. entry.author ..' }' 
  end
\stopluacode

\starttexdefinition btx:test
   \btxdirect{myauthor}
\stoptexdefinition

\startsetups btx:default:list:article
    \texdefinition{btx:test}
    \texdefinition{btx:default:title}
    \texdefinition{btx:default:journal}
    \texdefinition{btx:default:year}
    \removeunwantedspaces
    \removepunctuation
    \btxperiod
\stopsetups

\starttext

Citations: \cite[solo], \cite[co]

\startsubject[title=Bibliography]
\placelistofpublications[ref][method=dataset]
\stopsection

\stoptext

相关内容