我想知道是否可以设置一个命令,这样当调用\ref
Matlab时\lstinputlisting
,“Matlab 算法 XX”字样本身就会变成超链接,并且变成蓝色,而不必{\textbf{\textcolor{blue}{Matlab Algorithm \ref{XX}}}}
每次都输入该命令?
这是我的代码:
\documentclass[11pt]{book}
\usepackage{amsmath,amsfonts,amssymb,amsthm, bm}
\usepackage{xcolor}
\usepackage{listings}
\usepackage{filecontents}
\usepackage{hyperref}
\hypersetup{hidelinks,
colorlinks = true,
backref=true,
pagebackref=true,
hyperindex=true,
breaklinks=true,
urlcolor=blue,
citecolor = mybluei,
linkcolor = .,
bookmarks=true,
bookmarksopen=false,
pdftitle={Title},
pdfauthor={Author}}
\renewcommand{\lstlistingname}{\textbf{\textcolor{blue}{Matlab Algorithm}}}
\renewcommand{\lstlistlistingname}{List of \lstlistingname s}
\usepackage[numbered,framed]{matlab-prettifier}
\begin{filecontents}{mat1.m}
% arm1
x1 = [0 0]; y1 = [0 1];
x2 = [0 1]; y2 = [1 1];
x3 = [1 1]; y3 = [1 2];
% Size figure and draw arms
figure('position', [800, 300, 600, 550]);
hold on
plot(xcirc, ycirc,'m','linewidth', 1);
\end{filecontents}
\begin{document}
\lstinputlisting[
backgroundcolor=\color{blue!10},
style=Matlab-editor,
basicstyle=\mlttfamily\small,
caption={Draw a simple Fig.}
]{mat1.m}\label{mat1}
In the {\textbf{\textcolor{blue}{Matlab Algorithm \ref{mat1}}}} code generate above in we see how the arm moves.
\end{document}
答案1
如果不使用cleveref
,您可以定义一个新命令:
\newcommand{\matref}[1]{%
\hyperref[#1]{\textbf{\color{blue}Matlab Algorithm \ref{#1}}}%
}
然后,使用\matref{label}
而不是\ref{label}
来引用您的 Matlab 代码。
答案2
这使用cleveref
及其\crefformat
宏作为计数器,但是,如果与或listing
一起使用,它将应用于任何列表。\cref
\Cref
格式必须符合要求#2 Some text #1#3
才能使超链接正确。
\documentclass[11pt]{book}
\usepackage{amsmath,amsfonts,amssymb,amsthm, bm}
\usepackage{xcolor}
\usepackage{listings}
\usepackage{filecontents}
\usepackage{hyperref}
\usepackage{cleveref}
\hypersetup{hidelinks,
colorlinks = true,
backref=true,
pagebackref=true,
hyperindex=true,
breaklinks=true,
urlcolor=blue,
citecolor = mybluei,
linkcolor = .,
bookmarks=true,
bookmarksopen=false,
pdftitle={Title},
pdfauthor={Author}}
\renewcommand{\lstlistingname}{\textbf{\textcolor{blue}{Matlab Algorithm}}}
\renewcommand{\lstlistlistingname}{List of \lstlistingname s}
\crefname{listing}{matlab algorithm}{matlab algorithms}
\Crefname{listing}{Matlab Algorithm}{Matlab algorithms}
\crefformat{listing}{{\bfseries \color{blue}#2matlab algorithm~#1#3}}
\Crefformat{listing}{{\bfseries \color{blue}#2Matlab algorithm~#1#3}}
\usepackage[numbered,framed]{matlab-prettifier}
\begin{filecontents}{mat1.m}
% arm1
x1 = [0 0]; y1 = [0 1];
x2 = [0 1]; y2 = [1 1];
x3 = [1 1]; y3 = [1 2];
% Size figure and draw arms
figure('position', [800, 300, 600, 550]);
hold on
plot(xcirc, ycirc,'m','linewidth', 1);
\end{filecontents}
\begin{document}
\lstinputlisting[
backgroundcolor=\color{blue!10},
style=Matlab-editor,
basicstyle=\mlttfamily\small,
caption={Draw a simple Fig.},
label={mat1}
]{mat1.m}
\clearpage
In the \Cref{mat1} code generate above in we see how the arm moves and in \cref{mat1} we see the same!
In the {\textbf{\textcolor{blue}{Matlab Algorithm \ref{mat1}}}} code generate above in we see how the arm moves.
\end{document}