使用布尔函数 \ifthenelse 更改复杂参考书目设置的引用数

使用布尔函数 \ifthenelse 更改复杂参考书目设置的引用数

最后,感谢 moewe,我使用不同参考书目选项的示例运行良好。

在下面的例子中我需要添加布尔函数:

\usepackage{ifthen}

\newboolean{print_or_not} 
\setboolean{print_or_not}{true}
%\setboolean{print_or_not}{false}

并且在示例的末尾我通常(示例中的详细信息)添加条件\ifthenelse

\ifthenelse{\boolean{print_or_not}} { "打印所有参考书目" } { "不要在最后打印额外的参考书目" }

全部示例:

\documentclass[10pt,oneside,a4paper]{book}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\usepackage{ifthen}

\newboolean{print_or_not} 
\setboolean{print_or_not}{true}
% \setboolean{print_or_not}{false}

\usepackage[
  backend=biber,
  bibstyle=authoryear,
  citestyle=numeric,
  sorting=none,
  giveninits=true,
  dashed=false,
  refsegment=chapter,
  backref=true,
]{biblatex}

\makeatletter
\input{numeric.bbx}
\makeatother

\BiblatexSplitbibDefernumbersWarningOff

\DeclareSortingTemplate{none}{
  \sort{\field{usera}}
  \sort{\citeorder}
  \sort{\intciteorder}
}


\DeclareSourcemap{
  \maps[datatype=bibtex]{
    \map{
      \pertype{book}
      \step[fieldset=usera, fieldvalue=aa]
    }
    \map{
      \pertype{article}
      \step[fieldset=usera, fieldvalue=bb]
    }
    \map{
      \pertype{online}
      \step[fieldset=usera, fieldvalue=cc]
    }
    \map{
      \pertype{inproceedings}
      \step[fieldset=usera, fieldvalue=dd]
    }
  }
}

\DeclareNameAlias{author}{family-given}                         
\DeclareNameAlias{translator}{family-given}                         
\renewcommand*{\revsdnamepunct}{}


\defbibheading{subsubbibintoc}[\refname]{\subsection*{#1}} 
\defbibheading{subsubbibliography}[\refname]{\subsection*{#1}}  

\addbibresource{biblatex-examples.bib}

\begin{document}
\pagestyle{empty}
\tableofcontents
\chapter{First chapter}

Lorem \autocite{sigfridsson}
ipsum \autocite{nietzsche:ksa1}
dolor \autocite{moraux}
sit \autocite{knuth:ct:c}~\autocite{spiegelberg}.

\printbibheading[title=Chapter bibliography,heading=subbibintoc]
\printbibliography[segment=\therefsegment,type=book,heading=subsubbibintoc,title={Books}]
\printbibliography[segment=\therefsegment,type=article,heading=subsubbibintoc,title={Articles}]
\printbibliography[segment=\therefsegment,type=online,heading=subsubbibintoc,title={Online}]
\printbibliography[segment=\therefsegment,type=inproceedings,heading=subsubbibintoc,title={Conferences}]

\vspace{5mm}
{\let\clearpage\relax \chapter{Second chapter}}

Lorem~\cite{vizedom:related}
ipsum~\cite{gerhardt}
dolor~\cite{augustine}
sit~\cite{ctan}.

\printbibheading[title=Chapter bibliography,heading=subbibintoc]
\printbibliography[segment=\therefsegment,type=book,heading=subsubbibintoc,title={Books}]
\printbibliography[segment=\therefsegment,type=article,heading=subsubbibintoc,title={Articles}]
\printbibliography[segment=\therefsegment,type=online,heading=subsubbibintoc,title={Online}]
\printbibliography[segment=\therefsegment,type=inproceedings,heading=subsubbibintoc,title={Conferences}]

\vspace{5mm}
{\let\clearpage\relax \chapter{Third chapter}}

Lorem~\cite{wassenberg},
ipsum~\cite{baez/online}.

% COMENTING \ifthenelse ALL IS CORRECT  below
\ifthenelse{\boolean{print_or_not}}
{
% COMENTING above

\printbibheading[title=Chapter bibliography,heading=subbibintoc]
\printbibliography[segment=\therefsegment,type=book,heading=subsubbibintoc,title={Books}]
\printbibliography[segment=\therefsegment,type=article,heading=subsubbibintoc,title={Articles}]
\printbibliography[segment=\therefsegment,type=online,heading=subsubbibintoc,title={Online}]
\printbibliography[segment=\therefsegment,type=inproceedings,heading=subsubbibintoc,title={Conferences}]

\printbibheading[title=Global Bibliography,heading=bibintoc]
\printbibliography[type=book,heading=subsubbibliography,title={Books}]
\printbibliography[type=article,heading=subsubbibliography,title={Articles}]
\printbibliography[type=online,heading=subsubbibliography,title={Online}]
\printbibliography[type=inproceedings,heading=subsubbibliography,title={Conferences}]

\csletcs{saved@blx@refcontext@context}{blx@refcontext@context}
\newrefcontext[sorting=nyt]
\makeatletter
\AtNextBibliography{%
  \def\blx@setdefaultrefcontext#1{}%
  \csletcs{blx@refcontext@context}{saved@blx@refcontext@context}}
\makeatother
\printbibliography[title=Global Bibliography (alphabetic),heading=bibintoc]

\newrefcontext[sorting=nyt]
\makeatletter
\AtNextBibliography{%
  \def\blx@setdefaultrefcontext#1{}%
  \DeclareFieldFormat{labelnumberwidth}{#1\addperiod}}
\makeatother
\printbibliography[title={Global Bibliography (alphabetic \& continuous numbering)},heading=bibintoc]

% COMENTING  below
}
{
\printbibheading[title=DON'T PRINT BIBLIOGRAPHY,heading=bibintoc]
}
% COMENTING above

\end{document}

做出这一决定的原因很简单:我是否需要打印一份额外的全球书目。

问题是,如果部分代码采用布尔选项,如下所示

\ifthenelse{\boolean{print_or_not}}{}{}

引用数变化 [6] 为 [8] 等

在此处输入图片描述

在此处输入图片描述

....就像之前在主题中描述的那样\begingroup \endgroup 更改引用编号。当所有示例都无需布尔选项即可工作时,一切正常!

我们可以通过注释代码中的布尔选项或取消注释来检查这个问题。代码中指向的行是 104-105 和 138-141。

这种布尔选项引用错误的原因是什么?是否可以修复?这可能是我的“主题集”的最后一个严重问题。

答案1

这里的问题是,代码本质上可以归结为以下形式

\ifthenelse{\boolean{print_or_not}}
  {...
   \csletcs{saved@blx@refcontext@context}{blx@refcontext@context}
   \newrefcontext[sorting=nyt]
   \makeatletter
   \AtNextBibliography{%
     \def\blx@setdefaultrefcontext#1{}%
     \csletcs{blx@refcontext@context}{saved@blx@refcontext@context}}
   \makeatother
   \printbibliography[title=Global Bibliography (alphabetic),heading=bibintoc]

   \newrefcontext[sorting=nyt]
   \makeatletter
   \AtNextBibliography{%
     \def\blx@setdefaultrefcontext#1{}%
     \DeclareFieldFormat{labelnumberwidth}{#1\addperiod}}
   \makeatother
   \printbibliography[title={Global Bibliography (alphabetic \& continuous numbering)},heading=bibintoc]
   ...}
  {<print nothing>}

其中我们需要\makeatletter...\makeatother块,因为我们想要重新定义一个@名称中带有 的内部宏(\def\blx@setdefaultrefcontext#1{})。

但是,TeX 解析规则意味着在宏参数(参数是的分支)\makeatletter...\makeatother中使用时不起作用。(这与在脚注等中使用 和 朋友的效果相同。)<true>\ifthenelse{<condition>}{<true>}{<false>}verbatim

\makeatletter...\makeatother一个简单的解决方案是将宏调用移到外部

\makeatletter
\ifthenelse{\boolean{print_or_not}}
  {...
   \csletcs{saved@blx@refcontext@context}{blx@refcontext@context}
   \newrefcontext[sorting=nyt]
   \AtNextBibliography{%
     \def\blx@setdefaultrefcontext#1{}%
     \csletcs{blx@refcontext@context}{saved@blx@refcontext@context}}
   \printbibliography[title=Global Bibliography (alphabetic),heading=bibintoc]

   \newrefcontext[sorting=nyt]
   \AtNextBibliography{%
     \def\blx@setdefaultrefcontext#1{}%
     \DeclareFieldFormat{labelnumberwidth}{#1\addperiod}}
   \printbibliography[title={Global Bibliography (alphabetic \& continuous numbering)},heading=bibintoc]
   ...}
  {<print nothing>}
\makeatother

如果您使用以下形式的简单条件,也不会发生问题

\if<cond>
  <print bib>
\else
  <print nothing>
\fi

因为这样它就\makeatletter...\makeatother不会陷入宏观论证之中。

答案2

我已经找到解决办法。如果那么包我只是使用 \if \elsel \fi 函数\newif\ifprintornot\printornottrue变量的设置。此解决方案比使用如果那么包裹。

显然存在一些错误如果那么包。我仍然对使用 ifthen 包感兴趣,我会尝试向作者报告这个问题。如果有人找到 ifthen 包的解决方案,请在此处报告他的答案。

以下是所有已检查完毕、可立即使用的 100% 示例:

\documentclass[10pt,oneside,a4paper]{book}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}

\newif\ifprintornot
\printornottrue

\usepackage[
  backend=biber,
  bibstyle=authoryear,
  citestyle=numeric,
  sorting=none,
  giveninits=true,
  dashed=false,
  refsegment=chapter,
  backref=true,
]{biblatex}

\makeatletter
\input{numeric.bbx}
\makeatother

\BiblatexSplitbibDefernumbersWarningOff

\DeclareSortingTemplate{none}{
  \sort{\field{usera}}
  \sort{\citeorder}
  \sort{\intciteorder}
}


\DeclareSourcemap{
  \maps[datatype=bibtex]{
    \map{
      \pertype{book}
      \step[fieldset=usera, fieldvalue=aa]
    }
    \map{
      \pertype{article}
      \step[fieldset=usera, fieldvalue=bb]
    }
    \map{
      \pertype{online}
      \step[fieldset=usera, fieldvalue=cc]
    }
    \map{
      \pertype{inproceedings}
      \step[fieldset=usera, fieldvalue=dd]
    }
  }
}

\DeclareNameAlias{author}{family-given}                         
\DeclareNameAlias{translator}{family-given}                         
\renewcommand*{\revsdnamepunct}{}

\defbibheading    {subsubbibintoc}[\refname]{\subsection*{#1}} 
\defbibheading{subsubbibliography}[\refname]{\subsection*{#1}}  

\addbibresource{biblatex-examples.bib}

\begin{document}
\pagestyle{empty}
\tableofcontents
\chapter{First chapter}

Lorem \autocite{sigfridsson}
ipsum \autocite{nietzsche:ksa1}
dolor \autocite{moraux}
sit \autocite{knuth:ct:c}~\autocite{spiegelberg}.

\printbibheading[title=Chapter bibliography,heading=subbibintoc]
\printbibliography[segment=\therefsegment,type=book,heading=subsubbibintoc,title={Books}]
\printbibliography[segment=\therefsegment,type=article,heading=subsubbibintoc,title={Articles}]
\printbibliography[segment=\therefsegment,type=online,heading=subsubbibintoc,title={Online}]
\printbibliography[segment=\therefsegment,type=inproceedings,heading=subsubbibintoc,title={Conferences}]

\vspace{5mm}
{\let\clearpage\relax \chapter{Second chapter}}

Lorem~\cite{vizedom:related}
ipsum~\cite{gerhardt}
dolor~\cite{augustine}
sit~\cite{ctan}.

\printbibheading[title=Chapter bibliography,heading=subbibintoc]
\printbibliography[segment=\therefsegment,type=book,heading=subsubbibintoc,title={Books}]
\printbibliography[segment=\therefsegment,type=article,heading=subsubbibintoc,title={Articles}]
\printbibliography[segment=\therefsegment,type=online,heading=subsubbibintoc,title={Online}]
\printbibliography[segment=\therefsegment,type=inproceedings,heading=subsubbibintoc,title={Conferences}]

\vspace{5mm}
{\let\clearpage\relax \chapter{Third chapter}}

Lorem~\cite{wassenberg},
ipsum~\cite{baez/online}.

\ifprintornot
    
\printbibheading[title=Chapter bibliography,heading=subbibintoc]
\printbibliography[segment=\therefsegment,type=book,heading=subsubbibintoc,title={Books}]
\printbibliography[segment=\therefsegment,type=article,heading=subsubbibintoc,title={Articles}]
\printbibliography[segment=\therefsegment,type=online,heading=subsubbibintoc,title={Online}]
\printbibliography[segment=\therefsegment,type=inproceedings,heading=subsubbibintoc,title={Conferences}]

\printbibheading[title=Global Bibliography,heading=bibintoc]
\printbibliography[type=book,heading=subsubbibliography,title={Books}]
\printbibliography[type=article,heading=subsubbibliography,title={Articles}]
\printbibliography[type=online,heading=subsubbibliography,title={Online}]
\printbibliography[type=inproceedings,heading=subsubbibliography,title={Conferences}]

\csletcs{saved@blx@refcontext@context}{blx@refcontext@context}
\newrefcontext[sorting=nyt]
\makeatletter
\AtNextBibliography{%
  \def\blx@setdefaultrefcontext#1{}%
  \csletcs{blx@refcontext@context}{saved@blx@refcontext@context}}
\makeatother
\printbibliography[title=Global Bibliography (alphabetic),heading=bibintoc]

\newrefcontext[sorting=nyt]
\makeatletter
\AtNextBibliography{%
  \def\blx@setdefaultrefcontext#1{}%
  \DeclareFieldFormat{labelnumberwidth}{#1\addperiod}}
\makeatother
\printbibliography[title={Global Bibliography (alphabetic \& continuous numbering)},heading=bibintoc]

\else

\printbibheading[title=DON'T PRINT BIBLIOGRAPHY,heading=bibintoc]

\fi

\end{document}

一切都做得很好!我终于结束了我的漫长冒险使用 BiBLaTeX 中的不同类型的引用。

相关内容