Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I have a problem with my algorithms, I'm using the mcode-package: I would like the caption to say Algorithmus 1.1 for example. If I try it with a figure environment it does not work, because I don't have a pagebrak, when my algorithm ist very long. So, I googled, and came up with this solution:

\documentclass[german,11pt,twoside,a4paper,final,titlepage,openany]{amsbook}
\usepackage[ngerman]{babel}
\usepackage[latin1]{inputenc}
\usepackage[pdftex]{graphicx}
\usepackage{mathrsfs,amssymb}
\usepackage{amsbsy,amsmath}
\usepackage{float}
\usepackage{subcaption}
\usepackage[framed,numbered]{mcode}
\begin{document}

\begin{lstlisting}

function y = PolyValMatrix(p,x) 
    y = zeros(size(p,1),size(x,1));
    xt = x';
    for i = 1:size(p,2)
        n = size(p,2)-i;
        y = y + p(:,i)*(xt.^n);
    end
end

\end{lstlisting}
\renewcommand*\figurename{Algorithmus}
\captionof{figure}{Algorithmus}\label{algo:875}

\end{document} 

But the problem is: Once I insert another figure after this, it also calls it Algorithmus, and not Abbildung (figure) how I would like it.

Can anyone please help me? Sorry for the german words :)

Edit: This one is running correctly:

 \documentclass[german,11pt,twoside,a4paper,final,titlepage,openany]{amsbook}
 \usepackage[ngerman]{babel}
 \usepackage[latin1]{inputenc}    
 \usepackage{graphicx}               %% don't use [pdftex]
 \usepackage{mathrsfs,amssymb}
 \usepackage{amsbsy,amsmath}
 \usepackage{float}
 %\usepackage{subcaption}
 \usepackage[framed,numbered]{mcode}
\addto\captionsngerman{\renewcommand\lstlistingname{Algorithmus}}  %% Use this since
                                                               %%  you are using babel
\begin{document}
\begin{lstlisting}[float=htb,
                caption={Algorithmus\label{algo:875}},
                %captionpos=b    %% if you want caption below
                ]

function y = PolyValMatrix(p,x)
y = zeros(size(p,1),size(x,1));
xt = x';
    for i = 1:size(p,2)
    n = size(p,2)-i;
    y = y + p(:,i)*(xt.^n);
    end
end

\end{lstlisting}

Here we refer~\ref{algo:875}


\end{document} 

And this one isn't, but in my eyes it's exactly the same, apart from the not using float (but that doesn't make a difference...)

  \documentclass[german,11pt,twoside,a4paper,final,titlepage,openany]{amsbook}
  \usepackage[ngerman]{babel}
  \usepackage[latin1]{inputenc}
  \usepackage{graphicx}
  \usepackage{mathrsfs,amssymb}
  \usepackage{amsbsy,amsmath}
  \usepackage{float}
  %\usepackage{subcaption}
  \usepackage[framed,numbered]{mcode}
  \addto\captionsngerman{\renewcommand\lstlistingname{Algorithmus}}  %% Use this since

  \begin{document}
  Here we refer ~\ref{algo:875}
  \begin{lstlisting}[%float=htb,
                caption={Algorithmus\label{algo:875}},
                captionpos=b    %% if you want caption below
                      ]
  %Benötigte Funktionen
  function y = PolyValMatrix(p,x) 
      y = zeros(size(p,1),size(x,1));  
  xt = x';
  for i = 1:size(p,2)
        n = size(p,2)-i;
       y = y + p(:,i)*(xt.^n);

    end
end

\end{lstlisting}

Here we refer ~\ref{algo:875}
\end{document} 
share|improve this question
    
Put the redefinition in a group, or rather define a new environment, which can be done with tocbasic. –  Johannes_B Apr 2 at 7:42
    
Please also have a look at GoLaTeX, a german forum. –  Johannes_B Apr 2 at 7:43
    
Thanks for the advice, but you guys here always help me so fast :-) But if I do this as a new environment, I would have to choose figure for this as well, which would lead to my previous problem...Could you maybe be more specific? –  Nina Apr 2 at 7:54
    
Not begin another figure environment, but define a new one. Please see the link to golatex. –  Johannes_B Apr 2 at 7:56
    
You can also give a caption to the lstlisting environment –  Johannes_B Apr 2 at 7:56

1 Answer 1

mcode internally uses listings to typeset matlab codes. Hence you can add a caption using listings itself.

\documentclass[german,11pt,twoside,a4paper,final,titlepage,openany]{amsbook}
\usepackage[ngerman]{babel}
\usepackage[latin1]{inputenc}
\usepackage{graphicx}               %% don't use [pdftex]
\usepackage{mathrsfs,amssymb}
\usepackage{amsbsy,amsmath}
\usepackage{float}
%\usepackage{subcaption}
\usepackage[framed,numbered]{mcode}
\addto\captionsngerman{\renewcommand\lstlistingname{Algorithmus}}  %% Use this since
                                                                   %%  you are using babel
\begin{document}
\begin{lstlisting}[float=htb,
                    caption={Algorithmus},
                    label={algo:875},
                    %captionpos=b    %% if you want caption below
                    ]

function y = PolyValMatrix(p,x)
    y = zeros(size(p,1),size(x,1));
    xt = x';
    for i = 1:size(p,2)
        n = size(p,2)-i;
        y = y + p(:,i)*(xt.^n);
    end
end

\end{lstlisting}

Here we refer~\ref{algo:875}


\end{document}

enter image description here

share|improve this answer
    
Great,thanks for your help,that's exactly what I'm looking for! But there is a weird problem: My reference doesn't work: For me it doesn't say 1, it says 11. And in my original document it says 70. But when I click on that number it goes straight to the right spot... Do you know what that's about? –  Nina Apr 2 at 10:45
    
@Nina Glad it works. But I am getting the references correct. Can you compile 1-2 times more and see if it works? Also, did you run my exact code above. Note the \addto\captionsngerman command. –  Harish Kumar Apr 2 at 10:56
    
Ok, this is weird, or I am blind. I have one, that is running correctly and one that isn't. I will post both examples in my original post. –  Nina Apr 2 at 12:06
    
@Nina OK, my fault. Use caption={Algorithmus},label={algo:875},. This is the label command provided by listings. It works. I have updated the answer. –  Harish Kumar Apr 2 at 12:55
    
Ok, great, it works for me too! But it's weird though, that one time it works, one time it doesn't...Thanks a lot for your help! –  Nina Apr 2 at 13:14

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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