0

I am writing a MSc thesis in LaTeX and I would like to use a formatting style that comes close to that of published academic journal articles. This is my first big LaTeX project and I have only started using it fairly recently.

I have already searched for ways to fix my problem online, but I could not find a solution to accomplish my goal of setting up colorlinks (internal document cross-references) for authors and years using the apa-style and the biblatex package.

Here is my preamble:

%% 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%%

my compiled document looks like this:

enter image description here

Thank you very much in advance! The renewbibmacro workaround that I found on this platform has not worked for me unfortunately.

Sebastiano
  • 54,118
  • 1
    Welcome to tex.se. Put the renewbibmacro after the call to biblatex. – Cicada Feb 20 '20 at 06:57
  • 1
    Note that style=authoryear-comp, does not give you APA style. It just gives you author-year citations/bibliography. If you want real APA style you need style=apa, from biblatex-apa. – moewe Feb 20 '20 at 07:04
  • Related: https://tex.stackexchange.com/q/15951/35864, https://tex.stackexchange.com/q/27607/35864 – moewe Feb 20 '20 at 07:10
  • I had my style set to apa but then I would get the output that I have shown in the picture. the change in the style option to author year-comp and the whole renewbibmacro workaround was an attempt to fix the colorlinks. – Manu675 Feb 20 '20 at 08:00
  • I have tried to change the order of the renewbibmacro chunks of code and the biblatex package call but I still get error messages – Manu675 Feb 20 '20 at 08:00

1 Answers1

0

coloured cites

MWE

\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}

The example also uses \AtEveryCite{\color{red}}, to show the physical extent of the citation.

Cicada
  • 10,129
  • Note that the \renewbibmacros are intended for style=authoryear-comp, if you use them with style=authoryear, you may run into issues: Try \cite{knuth:ct:a,knuth:ct:b,knuth:ct:c} from biblatex-examples.bib. Note also that the redefinitions make ample use of deprecated bibmacros (there should be warnings in the .log file). – moewe Feb 20 '20 at 07:08
  • @moewe Yes, I noticed the style mismatch at the same time you made the comment. Have edited answer to style=authoryear-comp. There is a deprecation warning in the log: Warning: bibmacro 'cite:labelyear+extrayear' is deprecated, and then also a Using 'cite:labeldate+extradate' instead.... I remain deeply impressed with biblatex (and also Audrey, to whom the real answer belongs, according to the code comment). – Cicada Feb 20 '20 at 07:24