3

I use ShareLaTeX for my thesis and I am facing a problem. I have used the following code to change figures' name to "Εικόνα" and lstlistings' name to "Εικόνα" too.

\renewcommand{\lstlistingname}{Εικόνα}
\addto\captionsgreek{\renewcommand{\figurename}{Εικόνα}}

Now, the problem is that there may be a figure with name "Εικόνα 5.1" and also a listing with name "Εικόνα 5.1" because LateX uses different numbering for figures and lstlistings.

Is there a way to use a universal numbering for figures ang lstlistings?

2 Answers2

1

You could achieve universal numbering by defining a new counter and use that for your captions in listings and figure environments.

\documentclass{book}

\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage[english]{babel}
\usepackage[demo]{graphicx}
\usepackage{listings}

% Define new counter for listings and figures.
\newcounter{eikona}
\setcounter{eikona}{0}

% Define your listing style, if needed.
\lstdefinestyle{mystyle}{
    captionpos=b
}

% Define new listing environment that uses the custom counter
% and your listings style.
\lstnewenvironment{eikona}[1]{
    \renewcommand\lstlistingname{Figure}
    \setcounter{lstlisting}{\value{eikona}}
    \lstset{
        style=mystyle,
        caption={[#1]{#1}}
    }
}{\addtocounter{eikona}{1}}

% Change listings caption.
\renewcommand{\lstlistingname}{Figure}
\addto\captionsenglish{\renewcommand{\figurename}{Figure}}

% Change figure numbering.
\renewcommand\thefigure{\addtocounter{eikona}{1}\thechapter.\arabic{eikona}}


\begin{document}

\chapter{First chapter}

\clearpage
\begin{figure}[h]
    \centering
    \includegraphics{placeholder}
    \caption{This is a figure.}
\end{figure}

\begin{eikona}{Python example}
def hello():
    print "Hello!"
\end{eikona}

\begin{figure}[h]
    \centering
    \includegraphics{placeholder}
    \caption{Another figure.}
\end{figure}

\begin{figure}[h]
    \centering
    \includegraphics{placeholder}
    \caption{Another figure.}
\end{figure}

\begin{eikona}{Yet another Python example}
def bye():
    print "Goodbye!"
\end{eikona}

\end{document}

The code above should result in a document something like this:

Example of common numbering for listings and figures

vaiski
  • 111
  • This does not compile –  Apr 02 '16 at 11:44
  • @christian-hupfer Could you provide some details such as what errors do you get? Would be easier to provide help or fix the solution since I could succesfully compile it. One that comes to my mind is that you need to have the image named placeholder. – vaiski Apr 02 '16 at 12:18
  • Exactly: Use \usepackage[demo]{graphicx} to prevent such non-compilable examples –  Apr 02 '16 at 12:30
  • Ah, yes. Thanks for pointing that out! Had forgotten about the demo option since I prefer to use (in my view) visually more appealing placeholders. – vaiski Apr 02 '16 at 12:35
  • What can be more appealing like black boxes from the demo mode? ;-) –  Apr 02 '16 at 13:30
1

Here's a xassoccnt version of sharing/coupling counters that does not require more setup than defining a group of coupled counters, here figure and lstlisting. (This requires xassoccnt v0.6)

After that, the figure / listings counter step together.

\documentclass{book}

\usepackage[T1]{fontenc}
\usepackage{textgreek}
\usepackage[utf8]{inputenc}
\usepackage[greek,english]{babel}

\usepackage{listings}
\usepackage{xassoccnt}



\DeclareCoupledCounters[name=figurelistingsgroup]{figure,lstlisting}


\renewcommand{\lstlistingname}{Εικόνα}
\addto\captionsgreek{\renewcommand{\figurename}{Εικόνα}}

\begin{document}

\chapter{First}

\begin{figure}[ht]
    \caption{This is a figure.}
\end{figure}

\begin{lstlisting}[language=C,caption={Hello World},floatplacement={ht}]
#include<stdio.h>

int main(int argc,char **argv)
{
  printf("Hello World!\n");
  return(0);
}
\end{lstlisting}


\begin{figure}[ht]
    \caption{A dummy figure}
\end{figure}

\begin{figure}[ht]
    \caption{Yet another dummy figure}
\end{figure}

\begin{lstlisting}[caption={Hello World again},language=C,floatplacement={ht}]
#include<stdio.h>

int main(int argc,char **argv)
{
  printf("Hello World!\n");
  return(0);
}
\end{lstlisting}

\end{document}

enter image description here