列表中有自定义标题

列表中有自定义标题

我想为我的列表指定自定义名称,我该怎么做?这是我的代码:

\documentclass[12pt,a4paper]{scrreprt} 

\usepackage{listings} 
\lstloadlanguages{R} 
\lstset{% general command to set parameter(s) 
basicstyle=\sffamily\footnotesize, % print whole listing small 
keywordstyle=\sffamily\footnotesize\bfseries, % ubold black keywords 
identifierstyle=, % nothing happens 
commentstyle=\sffamily\footnotesize\slshape, % green comments 
stringstyle=\sffamily\footnotesize, % typewriter type for strings 
showstringspaces=false, % no special string spaces 
numbers=left, 
numberstyle=\sffamily\footnotesize, 
stepnumber=1, 
numbersep=10pt, 
showspaces=false, 
showtabs=false, 
frame=lines, 
morecomment=[l]{\%}, 
float=htbp, 
numberbychapter=true 
} 

\begin{document} 

\chapter{Kapitel2} 

Hier noch etwas Kode \ref{lst:DFZVf3}. 

\begin{lstlisting}[float,label={Code} ,caption=R Code für die Berechnung der nicht lieare Regression] 
#values of a time series with the length of 128 
data.frame(index=1:length(values),values)
fit.loess <- loess(values~index,df,span=.1)
plot(df, type="l", col="blue",main="loess")
lines(df$index,predict(fit.loess),col="red",lwd=2)
# non-linear regression usign a single sine term

fit.nls <- nls(values~a*sin(b*index+c)+d,df,
               start=c(a=1000,b=pi/10,c=0,d=mean(df$values)))
plot(df, type="l", col="blue",main="sin [1 term]")
lines(df$index,predict(fit.nls),col="red",lwd=2)



# non-linear regression using 2 sine terms
fit.nls <- nls(values~a1*sin(b1*index+c1)+a2*sin(b2*index+c2)+d,df,
               start=c(a1=1000,b1=pi/10,c1=1,
                       a2=1000,b2=pi/4.55,c2=1,d=mean(df$values)))
plot(df, type="l", col="blue",main="sin [2 terms]")
lines(df$index,predict(fit.nls),col="red",lwd=2) 
\end{lstlisting} 
\label{lst:DFZVf3} 

\end{document}

我希望codelisting

答案1

如果你babel使用 KOMA-Script 类加载包,则可以使用

\usepackage[ngerman]{babel}
\defcaptionname{ngerman}{\lstlistingname}{Code}
%\defcaptionname{ngerman}{\lstlistlistingname}{Codeverzeichnis}

请注意,label可选参数里面的键指的是代码:

Hier noch etwas Code \ref{lst:DFZVf3}.

\begin{lstlisting}[
  ...
  label={lst:DFZVf3},% this label refers to the code
  ...
] 

在此处输入图片描述

在此处输入图片描述

代码:

\documentclass[12pt]{scrreprt}

\usepackage{scrhack}% to avoid the warning that \float@addtolists is deprecated

\usepackage[utf8]{inputenc}
\usepackage[ngerman]{babel}
\defcaptionname{ngerman}{\lstlistingname}{Code}
\defcaptionname{ngerman}{\lstlistlistingname}{Codeverzeichnis}


\usepackage{listings} 
\lstloadlanguages{R} 
\lstset{% general command to set parameter(s) 
  basicstyle=\sffamily\footnotesize, % print whole listing small 
  keywordstyle=\sffamily\footnotesize\bfseries, % ubold black keywords 
  identifierstyle=, % nothing happens 
  commentstyle=\sffamily\footnotesize\slshape, % green comments 
  stringstyle=\sffamily\footnotesize, % typewriter type for strings 
  showstringspaces=false, % no special string spaces 
  numbers=left, 
  numberstyle=\sffamily\footnotesize, 
  stepnumber=1, 
  numbersep=10pt, 
  showspaces=false, 
  showtabs=false, 
  frame=lines, 
  morecomment=[l]{\%}, 
  float=htbp, 
  numberbychapter=true 
} 

\begin{document}

%\lstlistoflistings % list of listings

\chapter{Kapitel2} 
Hier noch etwas Code \ref{lst:DFZVf3}.

\begin{lstlisting}[
  float,
  label={lst:DFZVf3},% this label refers to the code
  caption={R Code für die Berechnung der nicht linearen Regression}
] 
#values of a time series with the length of 128 
data.frame(index=1:length(values),values)
fit.loess <- loess(values~index,df,span=.1)
plot(df, type="l", col="blue",main="loess")
lines(df$index,predict(fit.loess),col="red",lwd=2)
# non-linear regression usign a single sine term

fit.nls <- nls(values~a*sin(b*index+c)+d,df,
               start=c(a=1000,b=pi/10,c=0,d=mean(df$values)))
plot(df, type="l", col="blue",main="sin [1 term]")
lines(df$index,predict(fit.nls),col="red",lwd=2)



# non-linear regression using 2 sine terms
fit.nls <- nls(values~a1*sin(b1*index+c1)+a2*sin(b2*index+c2)+d,df,
               start=c(a1=1000,b1=pi/10,c1=1,
                       a2=1000,b2=pi/4.55,c2=1,d=mean(df$values)))
plot(df, type="l", col="blue",main="sin [2 terms]")
lines(df$index,predict(fit.nls),col="red",lwd=2) 
\end{lstlisting} 
\end{document}

答案2

\documentclass[12pt,a4paper]{scrreprt} 

\usepackage{listings} 
\lstloadlanguages{R} 
\lstnewenvironment
 {code}
 {\lstset{% general command to set parameter(s) 
  basicstyle=\sffamily\footnotesize, % print whole listing small 
  keywordstyle=\sffamily\footnotesize\bfseries, % ubold black keywords 
  identifierstyle=, % nothing happens 
  commentstyle=\sffamily\footnotesize\slshape, % green comments 
  stringstyle=\sffamily\footnotesize, % typewriter type for strings 
  showstringspaces=false, % no special string spaces 
  numbers=left, 
  numberstyle=\sffamily\footnotesize, 
  stepnumber=1, 
  numbersep=10pt, 
  showspaces=false, 
  showtabs=false, 
  frame=lines, 
  morecomment=[l]{\%}, 
  float=htbp, 
  numberbychapter=true}%
 }
{}
%\renewcommand*\lstlistingname{Code}%%% if needed
\begin{document} 

\chapter{Kapitel2} 

Hier noch etwas Kode \ref{lst:DFZVf3}. 

\begin{code}[float,label={Code} ,caption=R Code für die Berechnung der nicht lieare Regression] 
[ ...]
\end{code}
[...]

相关内容