2

I have two issues with this code: I want to cross-reference an equation as Eq(1) with color I want to cross-reference the section as well

any suggestions please, here the code and my outcome :

          \documentclass[a4paper, 11pt]{article} 
                    \usepackage{natbib}
                    \usepackage{amsmath}
                     \usepackage{amsthm} 
                    \usepackage[protrusion=true,expansion=true]{microtype} 
                   \usepackage{mathpazo} 
                   \usepackage{eucal}
                   \usepackage[mathcal]{eucal}
                    \usepackage[mathscr]{eucal}
                    \usepackage{mathrsfs}
                    \usepackage{lmodern}
                    \usepackage[a4paper]{geometry}
                     \usepackage[showframe]{geometry}
                     \usepackage{textcomp}
                     \usepackage{mathtools, cuted}
                      \usepackage{cuted}
                     \usepackage[utf8]{inputenc}
                     \usepackage[T1]{fontenc}
                      \usepackage[english]{babel}
                      \usepackage{booktabs,dcolumn,caption}
                    \usepackage[font=small,labelfont={bf,sf},tableposition=top]{caption}
                     \usepackage{booktabs,tabularx}
                              \usepackage[bottom]{footmisc}
                               \usepackage{empheq}
                              \usepackage{graphicx}
                               \usepackage{amssymb}
                               \usepackage{mathtools}
                                \usepackage{multirow}
                                 \usepackage{mathtools}
                                  \usepackage{array}
                                  \usepackage{booktabs}
                                  \usepackage[justification=centering]{caption}
                                  \usepackage{enumerate}
                                   \usepackage[english]{babel}
                                  \usepackage[latin1]{inputenc}     
                                  \usepackage[figurename=Fig.]{caption}
                                   \usepackage{tabulary}
                                    \usepackage{tabularx}
                                    \usepackage{varioref}
                                    \usepackage{multirow}
                                       \usepackage[skip=0.5\baselineskip,font=bf]{caption}
           \usepackage[rm={tabular, lining},sf={tabular, lining},tt{monowidth,tabular,lining {cfr-lm}
                \usepackage[onehalfspacing]{setspace}
                  \usepackage{rotating}
                        \usepackage{siunitx}
                       \usepackage{booktabs, makecell, multirow, threeparttable}
                        \usepackage{caption}
                        \usepackage{xparse}    
                         \usepackage{hyperref}
                         \usepackage[nameinlink,noabbrev]{cleveref} 
                          \usepackage{xstring}
\hypersetup{colorlinks = true, allcolors = blue}

\makeatletter

  \AtBeginDocument
   {
    \let\oldref\ref
     \renewcommand{\ref}[1]{\IfBeginWith{#1}{fig:}%
      {{\color{blue}Figure~\oldref{#1}}}%
      {\IfBeginWith{#1}{tab:}{{\color{blue}Table~\oldref{#1}}}{Unsupported ref start}}}%
        }
       \makeatother


      \begin{document}


          \begin{empheq}[left=\empheqlbrace]{align}
       Y_{t}    &= c+\sum_{j=1}^{P} \beta_{j}Y_{t-j}+ \sum_{k=1}^{K}b_{k} \widetilde{h}_{t-k}+ 
  \sigma^{*} exp\left({\frac{\widetilde{h_t}}{2}}\right)  \zeta_t  \label{eq:1}\\
  \widetilde{h}_{t} &= \alpha_{t}+\theta \widetilde{h}_{t-1}+\sum_{j=1}^{K} \delta_{j}Y_{t-j}+ \eta_t 
\end{empheq}

   The above equation \Cref{eq:1}, 

  In Section ~\ref{sec:data}, we describe the sample data. 

    \subsection{Data and Variables}
     \label{sec:data}

     \begin{table}[htp]
       \caption{Descriptive statistics}
        \label{tab1}

         \footnotesize
         \setlength{\tabcolsep}{0pt} 

          \begin{tabular*}{\textwidth}{
        @{\extracolsep{\fill}}
           l
           c
       S[table-format=3.4]
       S[table-format=3.3]
      S[table-format=4.4]
      S[table-format=-1.4]
      S[table-format=1.4]
      S[table-format=3.4]
      S[table-format=3.0]
         @{}
         }
     \toprule
   {Statistic} &
     {Mean} &
     {St. Dev.} &
     {Min} &
     {Max} &
     {Skewness} &
     {Kurtosis} & 
     {Jarque-Bera} &
      {N}\\
     \midrule
     GNP growth (y)          & 0.5531  &   0.7904  &  -2.65  &   2.09   & -1.3202  & 6.1412 &  79.991  
      & 114 \\
   GNP deflator (P)        & 0.6671  &   0.7647  &  -0.16  &   2.72   &  2.0248 & 7.2870 &  165.202 & 114 \\
 Unemployment (U)        & 6.0970  &   1.4535  &   3.90  &  10.53   &  0.9229 & 3.7673 &  18.982  & 114 \\
 BAA corporate bonds (S) & 2.3745  &   0.7904  &   1.08  &   5.82   &  1.5677 & 7.2624 &  133.001  & 114 \\
  House Price index (HPI) & 129.22  &  40.5193  &  75.30  & 202.53   &  0.0184 & 1.5491 &  10.004  & 114 \\
 S\&P 500   index        & 1173.83 & 561.4592  & 317.05  & 2732.58  &  0.4946 & 2.8919 &   4.704  & 114 \\
\bottomrule
 \end{tabular*}
 \end{table}

  Descriptive statistics of variables are given in \Cref{tab1}

 \end{document}

the outcome:

enter image description here

3
  • Please clarify what you intend to achieve by assigning a color to a cross-reference. E.g., should the cross-reference be made into a hyperlink? Or, do you want to assign colors for the sake of assigning colors? – Mico Apr 4 '20 at 19:35
  • Off-topic: Many of the instructions in your preamble are either mutually exclusive or redundant. It makes no sense to load the mathptmx package and later on the lmodern package, as the latter completely overrides the former. And, a few lines later, you load the cfr-lm package, which supersedes almost all preceding font-related packages. What exactly do you think you're going to achieve by loading the mathtools package three times and the caption package a whopping six times? Finally, please confirm that \usepackage[latin1]{} is meant to be a joke. – Mico Apr 4 '20 at 19:54
  • @Mico I want to assign a blue color when citing a section or equation – haithem Apr 5 '20 at 8:58
1

You wrote,

I want to assign a blue color when [cross-referencing] a section or equation.

You've already done most of the work: You're loading the hyperref package with the options colorlinks=true and allcolors=blue, and you're loading the cleveref package.

All you need to do now is to use \cref and \Cref consistently to generate cross-references to objects of type equation, section, and table.

Finally, I can't help but remark that the preamble of your test document is a mess. If this preamble is in any way representative of the preambles in your real documents, I wouldn't be surprised to learn that your documents barely compile. Some suggestions: (1) Don't load packages more than once, and certainly not 3 times or even a whopping 6 times (caption). (2) Don't load mutually conflicting and exclusive packages; e.g., there's is no value in loading first the mathptmx package, then the lmodern package, and then the cfr-lm package. Likewise, don't load the inputenc package twice -- once with the option utf8, the other time with the option latin1. (3) Explicitly specifying default options when loading a package, e.g., protrusion=true,expansion=true when loading microtype, does nothing except create code clutter. (4) Some packages load other packages automatically. For instance, mathtools loads amsmath automatically; hence, if your document loads the mathtools package, there is no need to load amsmath exclicitly.

In what follows, I've tried to remove the redundancies and outright conflicts from the preamble. Only you can know for sure if you really need packages such as cuted, xparsse, and xstrings.

enter image description here

  \documentclass[a4paper,11pt]{article} 
  \usepackage[utf8]{inputenc}
  \usepackage[T1]{fontenc}
  \usepackage{geometry}

  \usepackage[english]{babel}
  \usepackage{natbib}
  \usepackage{amssymb,mathtools,amsthm} 
  \usepackage{microtype} 
  \usepackage{textcomp}
  \usepackage{cuted}
  \usepackage{dcolumn}
  \usepackage[font=small,
              labelfont={bf,sf},
              tableposition=top,
              skip=0.5\baselineskip,
              font=bf,
              figurename=Fig.,
              justification=centering % that's the default
             ]{caption}
  \usepackage{mathrsfs} % provides \mathscr macro

  \usepackage[bottom]{footmisc}
  \usepackage{empheq}
  \usepackage{booktabs}
  \usepackage{enumerate} % don't you want 'enumitem'?
  \usepackage{tabularx,tabulary}
  \usepackage[rm={tabular,lining}, sf={tabular,lining},
              tt={monowidth,tabular,lining}]{cfr-lm}
  \usepackage[onehalfspacing]{setspace}
  \usepackage{rotating}
  \usepackage{siunitx}
  \usepackage{makecell,multirow,threeparttable}
  \usepackage{xparse,xstring}   

  \usepackage{varioref}
  \usepackage[colorlinks,allcolors=blue]{hyperref}
  \usepackage[nameinlink,noabbrev]{cleveref} 

\begin{document}
\setcounter{section}{2} % just for this example

\begin{equation}\label{eq:1} 1+1=2. \end{equation}
\subsection{Data and Variables} \label{sec:data}
\begin{table}[htp] \caption{Descriptive statistics}\label{tab1} \end{table}

% Finally, some cross-referncing commands:

As shown in \cref{eq:1} above, \dots

In \cref{sec:data}, we describe \dots 

Descriptive statistics are given in \Cref{tab1}.
\end{document}
1
  • 1
    Absolutely fantastic and very professional thanks – haithem Apr 5 '20 at 12:59
2

If you want to use cleveref features you should use them consequently.

For changing the cross-reference name cleveref provides the \crefname and \Crefname commands. You will find detailed information about the commands in the official package documentation.

The following example tries to illustrate the use of the commands:

\documentclass[a4paper, 11pt]{article} 
\usepackage[english]{babel}
\usepackage{empheq}
\usepackage[a4paper]{geometry}
\usepackage{siunitx}
\usepackage{booktabs}
\usepackage{hyperref}
\hypersetup{colorlinks = true, allcolors = blue}
\usepackage[nameinlink,noabbrev]{cleveref} 

\crefname{equation}{eq.}{eq.}

\setlength\parindent{0pt}

\begin{document}

\begin{empheq}[left=\empheqlbrace]{align}
    Y_{t}    &= c+\sum_{j=1}^{P} \beta_{j}Y_{t-j}+ \sum_{k=1}^{K}b_{k} \widetilde{h}_{t-k}+ 
        \sigma^{*} exp\left({\frac{\widetilde{h_t}}{2}}\right)  \zeta_t  \label{eq:1}\\
    \widetilde{h}_{t} &= \alpha_{t}+\theta \widetilde{h}_{t-1}+\sum_{j=1}^{K} \delta_{j}Y_{t-j}+ \eta_t 
\end{empheq}

\Cref{eq:1} is equal to equation \cref{eq:1}.

In ~\cref{sec:data}, we describe the sample data. 

\subsection{Data and Variables}
\label{sec:data}

\begin{table}[htp]
    \caption{Descriptive statistics}\medskip
    \label{tab1}
    \footnotesize
    \setlength{\tabcolsep}{0pt} 

    \begin{tabular*}{\textwidth}{@{\extracolsep{\fill}}lcS[table-format=3.4]S[table-format=3.3]S[table-format=4.4]S[table-format=-1.4]S[table-format=1.4]S[table-format=3.4]S[table-format=3.0]@{}}
        \toprule
        {Statistic} & {Mean} & {St. Dev.} & {Min} & {Max} & {Skewness} & {Kurtosis} & {Jarque-Bera} & {N}\\
        \midrule
        GNP growth (y)          & 0.5531  &   0.7904  &  -2.65  &   2.09   & -1.3202  & 6.1412 &  79.991  & 114 \\
        GNP deflator (P)        & 0.6671  &   0.7647  &  -0.16  &   2.72   &  2.0248 & 7.2870 &  165.202 & 114 \\
        Unemployment (U)        & 6.0970  &   1.4535  &   3.90  &  10.53   &  0.9229 & 3.7673 &  18.982  & 114 \\
        BAA corporate bonds (S) & 2.3745  &   0.7904  &   1.08  &   5.82   &  1.5677 & 7.2624 &  133.001  & 114 \\
        House Price index (HPI) & 129.22  &  40.5193  &  75.30  & 202.53   &  0.0184 & 1.5491 &  10.004  & 114 \\
        S\&P 500   index        & 1173.83 & 561.4592  & 317.05  & 2732.58  &  0.4946 & 2.8919 &   4.704  & 114 \\
        \bottomrule
    \end{tabular*}
\end{table}

Descriptive statistics of variables are given in \cref{tab1}

\end{document}

enter image description here

1
  • I think you placed the comment at the wrong place :) – user1146332 Apr 4 '20 at 19:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.