I have a lot of subsections with the name "Example X.Y" in my thesis and want to use a macro which generates the names and labels for the subsections.
The goal is to be able to reference examples like in the picture below:
Using the nameref package I tried the following:
\documentclass[10pt,a4paper]{report}
\usepackage[utf8]{inputenc}
\usepackage[english]{babel}
\usepackage[T1]{fontenc}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{nameref}
\newcommand{\example}{\subsection{Example \arabic{section}.\arabic{subsection} }\label{ex:\arabic{section}-\arabic{subsection}}}
\begin{document}
\chapter{Simulations}
\section{Examples 1}
\example
\example
\nameref{ex:1-2} has the same solution as \nameref{ex:1-1}.
\end{document}
This however gives me the text "Example 1.2 has the same solution as Example 1.2".
I suspect that the reference is working as intended, but when substituting the subsection's name Example \arabic{section}.\arabic{subsection} is called again, resulting in only giving me the name of the current example/subsection.
Is there a way to generate subsections and labels with a macro or do I have to do it all by hand?


.auxfile that the counter values are not expanded in the\newlabelleading toExample \arabic{section}.\arabic{subsection}being the value of both labels. – moewe May 25 '18 at 08:10Example~1.2instead of\nameref{ex:1-2}, as you need to know the actual number? – egreg May 25 '18 at 15:45