使用 apa 样式通过 biblatex 创建作者和年份的颜色链接

使用 apa 样式通过 biblatex 创建作者和年份的颜色链接

我正在撰写硕士论文LaTeX,我想使用一种接近已发表学术期刊文章的格式。这是我的第一个大LaTeX项目,我最近才开始使用它。

我已经在线搜索了解决问题的方法,但我找不到解决方案来实现使用 apa 样式和包为作者和年份设置颜色链接(内部文档交叉引用)的目标biblatex

以下是我的序言:

%% begin preamble%%

% set the document class
\documentclass[12pt]{report}

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

%package to create a list of acronyms which appears in the table of contents
\usepackage[acronym, toc, nopostdot, nogroupskip]{glossaries}

% acronym list entries:
\newacronym{mbb}{MBB}{moving block bootstrap}
\newacronym{rss}{RSS}{residual sum of squares}
\newacronym{mae}{MAE}{mean absolute error}
\newacronym{rmse}{RMSE}{root mean squared error}
\newacronym{ann}{ANN}{Artificial Neural Network}
\newacronym{dl}{DL}{Deep Learning}
\newacronym{mse}{MSE}{mean squared error}
\newacronym{lstmnn}{LSTM NN}{Long Short Term Memory neural network}
\newacronym{rnn}{RNN}{Recurrent Neural Network}
\newacronym{arima}{ARIMA}{Autoregressive Integrated Moving Average}

% end acronym list entries

\makenoidxglossaries 

% aligns the description entries in the list of acronyms
\glsfindwidesttoplevelname
\setglossarystyle{alttree}

% setting the space between acronym and description
\renewcommand\glstreepredesc{\qquad} 


% prepare the document for images
\usepackage{graphicx}
% sets the directory where images are stored
\graphicspath{ {images/} }

% set spacing package to change the spacing for the table of contents
\usepackage{setspace}

% package to create captions and subcaptions for figures and tables
\usepackage[labelfont=bf]{caption}
\usepackage{subcaption}

% package to include notes underneath figures/tables
\usepackage{floatrow}

% package to type piecewise functions
\usepackage{amsmath}

% color package for formatting URLs 
\usepackage[dvipsnames]{xcolor}

% packages for cross references within the Latex document
\usepackage{varioref}

% package to create hyperlinks within the Latex document (including the bibliography if url is specified)

\usepackage[backref=true]{hyperref}

% the cleverref package   enhances   LATEX’s   cross-referencing   features,allowing the format of cross-references to be determined automatically ac-cording  to  the  “type”  of  cross-reference  (equation,  section,  etc.)   and  thecontext in which the cross-reference is used
\usepackage[capitalise, noabbrev]{cleveref}

% formatting the URLs
\hypersetup{colorlinks=true, linkcolor=BrickRed, urlcolor=RoyalBlue, citecolor=BlueViolet}
\urlstyle{same}


% using the geometry package to set margins of the document

\usepackage[a4paper,width=150mm,top=25mm,bottom=25mm]{geometry}

% creating the option to display bookmarks in the compiled pdf document
\usepackage{bookmark}

% using packages to add in customized headers and footers

\usepackage{fancyhdr}
\pagestyle{fancy}

% separation lines for header and footer


% specifying inputs for headers and footers (commented out for now)


\fancyhead[RO,LE]{Chapter \thechapter}
\fancyfoot[C]{\thepage}


% changing the thickness of the lines in the headers and footers:

\renewcommand{\headrulewidth}{0 pt}
\renewcommand{\footrulewidth}{0 pt}

% creating colorlinks to the names of the authors AND the year:

% Citation Hyperlinks (not just years), thanks to Audrey.
\makeatletter
\renewbibmacro*{cite}{% Based on cite bib macro from authoryear-comp.cbx
     \iffieldundef{shorthand}
       {\ifthenelse{\ifnameundef{labelname}\OR\iffieldundef{labelyear}}
          {\printtext[bibhyperref]{% Include labelname in hyperlink
             \DeclareFieldAlias{bibhyperref}{default}% Prevent nested hyperlinks
             \usebibmacro{cite:label}%
             \setunit{\addspace}%
             \usebibmacro{cite:labelyear+extrayear}}%
             \usebibmacro{cite:reinit}}
          {\iffieldequals{namehash}{\cbx@lasthash}
             {\ifthenelse{\iffieldequals{labelyear}{\cbx@lastyear}\AND
                          \(\value{multicitecount}=0\OR\iffieldundef{postnote}\)}
                {\setunit{\addcomma}%
                 \usebibmacro{cite:extrayear}}
                {\setunit{\compcitedelim}%
                 \usebibmacro{cite:labelyear+extrayear}%
                 \savefield{labelyear}{\cbx@lastyear}}}
             {\printtext[bibhyperref]{% Include labelname in hyperlink
                \DeclareFieldAlias{bibhyperref}{default}% Prevent nested hyperlinks
                \printnames{labelname}%
                \setunit{\nameyeardelim}%
                \usebibmacro{cite:labelyear+extrayear}}%
                \savefield{namehash}{\cbx@lasthash}%
                \savefield{labelyear}{\cbx@lastyear}}}}
       {\usebibmacro{cite:shorthand}%
        \usebibmacro{cite:reinit}}%
     \setunit{\multicitedelim}}

\renewbibmacro*{textcite}{% Based on textcite bib macro from authoryear-comp.cbx
     \iffieldequals{namehash}{\cbx@lasthash}
       {\iffieldundef{shorthand}
          {\ifthenelse{\iffieldequals{labelyear}{\cbx@lastyear}\AND
                       \(\value{multicitecount}=0\OR\iffieldundef{postnote}\)}
             {\setunit{\addcomma}%
              \usebibmacro{cite:extrayear}}
             {\setunit{\compcitedelim}%
              \usebibmacro{cite:labelyear+extrayear}%
              \savefield{labelyear}{\cbx@lastyear}}}
          {\setunit{\compcitedelim}%
           \usebibmacro{cite:shorthand}%
           \global\undef\cbx@lastyear}}
       {\ifnameundef{labelname}
          {\printtext[bibhyperref]{% Include labelname in hyperlink
             \DeclareFieldAlias{bibhyperref}{default}% Prevent nested hyperlinks
             \iffieldundef{shorthand}
               {\usebibmacro{cite:label}%
                \setunit{%
                  \global\booltrue{cbx:parens}%
                  \addspace\bibopenparen}%
                \ifnumequal{\value{citecount}}{1}
                  {\usebibmacro{prenote}}
                  {}%
                \usebibmacro{cite:labelyear+extrayear}}
               {\usebibmacro{cite:shorthand}}%
             \ifthenelse{\iffieldundef{postnote}\AND
                         \(\value{multicitetotal}=0\AND\value{citetotal}=1\)}
               {\bibcloseparen% Include closing parenthesis in hyperlink
                \global\boolfalse{cbx:parens}}
               {}}}
          {\printtext[bibhyperref]{% Include labelname in hyperlink
             \DeclareFieldAlias{bibhyperref}{default}% Prevent nested hyperlinks
             \printnames{labelname}%
             \setunit{%
               \global\booltrue{cbx:parens}%
               \addspace\bibopenparen}%
             \ifnumequal{\value{citecount}}{1}
               {\usebibmacro{prenote}}
               {}%
             \iffieldundef{shorthand}
               {\iffieldundef{labelyear}
                  {\usebibmacro{cite:label}}
                  {\usebibmacro{cite:labelyear+extrayear}}%
                \savefield{labelyear}{\cbx@lastyear}}
               {\usebibmacro{cite:shorthand}%
                \global\undef\cbx@lastyear}%
             \ifthenelse{\iffieldundef{postnote}\AND
                         \(\value{multicitetotal}=0\AND\value{citetotal}=1\)}
               {\bibcloseparen% Include closing parenthesis in hyperlink
                \global\boolfalse{cbx:parens}}
               {}}%
             \savefield{namehash}{\cbx@lasthash}}}%
     \setunit{%
       \ifbool{cbx:parens}
         {\bibcloseparen\global\boolfalse{cbx:parens}}
         {}%
       \multicitedelim}}

\makeatother

% using the biblatex package to create references

\usepackage[style=authoryear-comp ,sorting=nyt, maxcitenames=2, natbib=true,backend=biber]{biblatex}
\addbibresource{references.bib}



% This allows for an additional line-breaking pass with the amount of "tolerable" white space per line increased by 1em.
\emergencystretch=1em



%% end preamble%%

我的编译文档如下所示:

在此处输入图片描述

提前谢谢您!renewbibmacro遗憾的是,我在这个平台上找到的解决方法对我没有用。

答案1

彩色城市

平均能量损失

\documentclass{article}

    \begin{filecontents}{\jobname.bib}
    @misc{john-73,
        author = {John, S.},
        year = {1973},
        title = {The best book ever},
    }
    \end{filecontents}


%== use color in citations ==%
\usepackage[
            colorlinks=true,        
            allcolors = black,  
            citecolor=blue,        
        ]{hyperref} 

\usepackage[style=authoryear-comp]{biblatex}
\addbibresource{\jobname.bib}



\AtEveryCite{\color{red}}




% creating colorlinks to the names of the authors AND the year:

% Citation Hyperlinks (not just years), thanks to Audrey.
\makeatletter
\renewbibmacro*{cite}{% Based on cite bib macro from authoryear-comp.cbx
     \iffieldundef{shorthand}
       {\ifthenelse{\ifnameundef{labelname}\OR\iffieldundef{labelyear}}
          {\printtext[bibhyperref]{% Include labelname in hyperlink
             \DeclareFieldAlias{bibhyperref}{default}% Prevent nested hyperlinks
             \usebibmacro{cite:label}%
             \setunit{\addspace}%
             \usebibmacro{cite:labelyear+extrayear}}%
             \usebibmacro{cite:reinit}}
          {\iffieldequals{namehash}{\cbx@lasthash}
             {\ifthenelse{\iffieldequals{labelyear}{\cbx@lastyear}\AND
                          \(\value{multicitecount}=0\OR\iffieldundef{postnote}\)}
                {\setunit{\addcomma}%
                 \usebibmacro{cite:extrayear}}
                {\setunit{\compcitedelim}%
                 \usebibmacro{cite:labelyear+extrayear}%
                 \savefield{labelyear}{\cbx@lastyear}}}
             {\printtext[bibhyperref]{% Include labelname in hyperlink
                \DeclareFieldAlias{bibhyperref}{default}% Prevent nested hyperlinks
                \printnames{labelname}%
                \setunit{\nameyeardelim}%
                \usebibmacro{cite:labelyear+extrayear}}%
                \savefield{namehash}{\cbx@lasthash}%
                \savefield{labelyear}{\cbx@lastyear}}}}
       {\usebibmacro{cite:shorthand}%
        \usebibmacro{cite:reinit}}%
     \setunit{\multicitedelim}}

\renewbibmacro*{textcite}{% Based on textcite bib macro from authoryear-comp.cbx
     \iffieldequals{namehash}{\cbx@lasthash}
       {\iffieldundef{shorthand}
          {\ifthenelse{\iffieldequals{labelyear}{\cbx@lastyear}\AND
                       \(\value{multicitecount}=0\OR\iffieldundef{postnote}\)}
             {\setunit{\addcomma}%
              \usebibmacro{cite:extrayear}}
             {\setunit{\compcitedelim}%
              \usebibmacro{cite:labelyear+extrayear}%
              \savefield{labelyear}{\cbx@lastyear}}}
          {\setunit{\compcitedelim}%
           \usebibmacro{cite:shorthand}%
           \global\undef\cbx@lastyear}}
       {\ifnameundef{labelname}
          {\printtext[bibhyperref]{% Include labelname in hyperlink
             \DeclareFieldAlias{bibhyperref}{default}% Prevent nested hyperlinks
             \iffieldundef{shorthand}
               {\usebibmacro{cite:label}%
                \setunit{%
                  \global\booltrue{cbx:parens}%
                  \addspace\bibopenparen}%
                \ifnumequal{\value{citecount}}{1}
                  {\usebibmacro{prenote}}
                  {}%
                \usebibmacro{cite:labelyear+extrayear}}
               {\usebibmacro{cite:shorthand}}%
             \ifthenelse{\iffieldundef{postnote}\AND
                         \(\value{multicitetotal}=0\AND\value{citetotal}=1\)}
               {\bibcloseparen% Include closing parenthesis in hyperlink
                \global\boolfalse{cbx:parens}}
               {}}}
          {\printtext[bibhyperref]{% Include labelname in hyperlink
             \DeclareFieldAlias{bibhyperref}{default}% Prevent nested hyperlinks
             \printnames{labelname}%
             \setunit{%
               \global\booltrue{cbx:parens}%
               \addspace\bibopenparen}%
             \ifnumequal{\value{citecount}}{1}
               {\usebibmacro{prenote}}
               {}%
             \iffieldundef{shorthand}
               {\iffieldundef{labelyear}
                  {\usebibmacro{cite:label}}
                  {\usebibmacro{cite:labelyear+extrayear}}%
                \savefield{labelyear}{\cbx@lastyear}}
               {\usebibmacro{cite:shorthand}%
                \global\undef\cbx@lastyear}%
             \ifthenelse{\iffieldundef{postnote}\AND
                         \(\value{multicitetotal}=0\AND\value{citetotal}=1\)}
               {\bibcloseparen% Include closing parenthesis in hyperlink
                \global\boolfalse{cbx:parens}}
               {}}%
             \savefield{namehash}{\cbx@lasthash}}}%
     \setunit{%
       \ifbool{cbx:parens}
         {\bibcloseparen\global\boolfalse{cbx:parens}}
         {}%
       \multicitedelim}}

\makeatother



\begin{document}

``Some quotation'' \cite[43]{john-73}.
\printbibliography

 \end{document}

该示例还使用\AtEveryCite{\color{red}}, 来显示引用的物理范围。

相关内容