2

Thanks to the answer, which suggested me to use \cref{} to refer sections, definitions etc.

I am using \begin{equation} \end{equation} for both equations and diagrams. So, when I use \cref{}, I am getting Equation (reference number) for diagrams. Is there a way to fix it?

Please find the minimal working example after suggestion of Willie Wong

\documentclass[11pt,twoside]{book}

%%% Packages
% 1. fonts
\usepackage[T1]{fontenc} % recommended
%\usepackage[utf8]{inputenc} % no longer needed
\usepackage{fix-cm} % for Computer Modern (comment if using the next line)
%\usepackage{newpxtext,newpxmath} % for Palatino
%\usepackage{upgreek} % do you need it?
%\usepackage{charter} % it has no support for math
%\usepackage{relsize} % do you need it?
\usepackage{a4wide}
% 2. Math
\usepackage{stmaryrd}
\usepackage{mathrsfs}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{mathtools}
\usepackage{bm}% bold math
\usepackage[normalem]{ulem}
% 3. Graphics and special effects
\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{cd}
\usepackage{tikz-cd}
\usepackage{shadow}% do you need it?
\usepackage{fancybox}% do you need it?
\usepackage{scalerel,stackengine}
\usepackage{eso-pic}

% 4. Document formatting
\usepackage{setspace}
\usepackage{fancyhdr}
\usepackage{appendix}
\usepackage[nottoc]{tocbibind}
\usepackage{alltt}
\usepackage{amsthm}


\usepackage[colorlinks]{hyperref}
\usepackage[nameinlink]{cleveref}

\newcounter{diagram}
\newenvironment{diagram}{\setcounter{diagram}{\value{equation}}\refstepcounter{diagram}}{}

\begin{document}
    
    \pagenumbering{roman}
    
    \include{Title} 
    
    \newpage
    \thispagestyle{empty}
    \bigskip{}
    \vspace{1cm} \cleardoublepage 
    
    
    
    \begin{diagram}\label{diagram:diagram}
        \begin{equation}
            \begin{tikzcd}[sep=large]
                A \arrow[dd] \arrow[rrr]  & & & B \arrow[dd] \\
                & & &       \\
                C \arrow[rrr]  & & & D   
            \end{tikzcd}    
        \end{equation}
    \end{diagram}
    
    
        \begin{diagram}
        \begin{equation}\label{diagram:diagram1}
            \begin{tikzcd}[sep=large]
                A \arrow[dd] \arrow[rrr]  & & & B \arrow[dd] \\
                & & &       \\
                C \arrow[rrr]  & & & D   
            \end{tikzcd}    
        \end{equation}
    \end{diagram}

    \begin{equation}\label{eqn:equation}
        y = mx + c
    \end{equation}
    
    \Cref{diagram:diagram}
    \Cref{diagram:diagram1}
    \Cref{eqn:equation}
    
    
    
    
\end{document}

Please find attached output

enter image description here

6
  • I am not sure what is giving you the error. Your MWE is super long and it will take someone a bit of time to find the error. But try the following. change the definition of the diagram environment to \newenvironment{diagram}{\setcounter{diagram}{\value{equation}}\refstepcounter{diagram}}{}. It is a lot less fancy: to use it you just put \begin{diagram}... \end{diagram} around the \begin{equation}\begin{tikzcd}...\end{tikzcd}\end{equation} that you already have. But move the \label to before \begin{equation}. Commented May 19, 2021 at 3:03
  • @WillieWong PLease find the edit. It is showing error, one of the lines being "No counter 'diagram' defined. \begin{diagram}". Commented May 19, 2021 at 3:50
  • 1
    well you are missing the \newcounter{diagram} from the answer. Commented May 19, 2021 at 7:18
  • @UlrikeFischer Yes :) Sorry.. I added now.. Still having some trouble... Commented May 19, 2021 at 13:52
  • @PraphullaKoushik: you also need to make cleveref aware of the new counter. The answer provided by Willoughby has three lines that start with \cref... you need also those three lines. (Paste them in immediately after the \newenvironment line. Commented May 21, 2021 at 1:51

4 Answers 4

4
+100

enter image description here

We can define a new diagram environment and make it work with cleveref. I assumed that you want the counter to be the same for both equations and diagrams.

\documentclass{article}
\usepackage{tikz}
\usetikzlibrary{cd}

\usepackage[colorlinks]{hyperref}
\usepackage[nameinlink]{cleveref}

\newcounter{diagram}  % Create counter for diagrams

% Create diagram environment
\newenvironment{diagram}[1][]{%
    \begin{equation}%
    \begin{tikzcd}[#1]%
    \setcounter{diagram}{\theequation}
    \addtocounter{diagram}{-1}
    \refstepcounter{diagram}
}{%
    \end{tikzcd}%
    \end{equation}%
}
% Add cleveref information for diagram
\crefname{diagram}{diagram}{diagrams}
\crefname{diagram}{Diagram}{Diagrams}
\creflabelformat{diagram}{(#1) #2 #3}

\begin{document}
    \begin{equation}\label{eqn:equation}
        y = mx + c
    \end{equation}
    
    \begin{diagram}[row sep=2cm, column sep=3cm] \label{dia:diagram}
        A \arrow[d] \arrow[r] & B \arrow[d]\\
        C \arrow[r] & D
    \end{diagram}
    \Cref{eqn:equation} \Cref{dia:diagram}
\end{document}

Explanation

First we define a new counter, diagram. Whenever cleveref 'sees' this counter it will know that we want a diagram label. We then define an environment, diagram. This includes a tikzcd environment inside a equation environment. The equation environment ensures that the numbering and spacing is the same as for equations. Note that any optional arguments passed to the diagram environment are passed on to the tikzcd environment. We then set the counter diagram to be the same value as the equation counter. We then subtract one and then in a single step increment and reference the diagram counter (this is why we needed to subtract one first). By doing this after the equation environment starts we ensure that the \label command 'sees' the diagram counter. We then end the environment by closing the two environments we opened.

To make cleveref work with the diagram environment there are a couple of things we have to do. First we need to tell cleveref what word to use in place of 'Equation', this is done with

\crefname{<type>}{<singular>}{<plural>}

in this case <type> is the counter we are referencing, so diagram and <singular>/<plural> are the singular/plural form of the word. The next line is the same but uses \Crefname which defines the capitalised version of the word. We could get away with defining just upper or just lower case and then cleveref will work out the other version but its simple enough to define both and if the name was more complicated then cleveref may fail to find the correct capitalisation. The final thing we have to do is use

\creflabelformat{<type>}{<format>}

to tell cleveref how to print the label, in this case <type> is the counter diagram again and format is (#1) #2 #3 which puts brackets around the number and the other two have to do with getting hyperref to work with clveref, which can be seen from the red links in the image (simply remove colorlinks from the hyperref options to get rid of the colour).

I've taken the liberty of removing blank lines in the example diagram and instead setting row sep and column sep.

10
  • for some reason, it is not working. I am still getting Equation (reference number) Commented May 19, 2021 at 0:16
  • 1
    Without seeing your code its not possible to say why, I suggest you post a new question and put all of your code in it asking why it doesn't work. As an aside your 'minimal working example (MWE)' is not very minimal, take out any code that isn't needed to compile the document and reproduce the issue.
    – Willoughby
    Commented May 19, 2021 at 0:23
  • I am using \begin{tikzcd}\end{tikzcd} for diagrams. Is it not different from \begin{diagram} \end{diagram}? Commented May 19, 2021 at 0:26
  • The way that I've defined the diagram environment above includes the tikzcd environment, notice in the example usage that there is no tikzcd explicitly in the main body of the document but the definition of diagram includes \begin{tikzcd} and \end{tikzcd}. If this is undesirable you can remove the tikzcd environment \begin and \end from the definition of diagram and then use them in the document nesting them as \begin{diagram} \begin{tikzcd} ... \end{tikzcd} \end{diagram}. So replace all \begin{equation}\begin{tikzcd} with \begin{diagram} and do the same for \end...
    – Willoughby
    Commented May 19, 2021 at 0:31
  • I am using \documentclass[11pt,twoside]{book}. Sorry for not mentioning it before. Would that be a reason for not working? I have added the code I am using. \begin{diagram} \end{diagram} is not even working. DId I place them at a wrong place? Commented May 19, 2021 at 0:40
2

When a counter is printed (in a caption, equation number, or using \ref or \cref) LaTeX uses the internal macro \thecountername for the various counter names, for example \theequation for the equation number and \thechapter for the chapter number.

This macro is the connection between the counter and the output in the document, the counter itself is an internal variable and the macro controls how this variable is printed: in arabic or roman numerals for example, but also if there is any other information added in the printed form, such as the section or chapter number.

When you make a new counter with \newcounter{diagram} then LaTeX automatically creates the macro \thediagram, which is defined by default as \arabic{diagram}, so the arabic number and nothing else. This definition is picked up by \Cref, so only (1) is shown. However, it is possible to redefine \thediagram yourself after you make the counter:

\newcounter{diagram}
\def\thediagram{\thechapter.\arabic{diagram}}

In this definition the chapter counter is printed first, then a period, then the arabic representation of the diagram counter itself. This is very similar to how \theequation is defined by LaTeX itself.

Full MWE:

\documentclass[11pt,twoside]{book}

\usepackage{amsmath}
\usepackage{tikz-cd}

\usepackage[colorlinks]{hyperref}
\usepackage[nameinlink]{cleveref}

\newcounter{diagram}
\def\thediagram{\thechapter.\arabic{diagram}}

\newenvironment{diagram}{\setcounter{diagram}{\value{equation}}\refstepcounter{diagram}}{}

\crefname{diagram}{diagram}{diagrams}
\crefname{diagram}{Diagram}{Diagrams}
\creflabelformat{diagram}{(#1)#2#3}

\begin{document}
    
\chapter{Diagrams and equations}

\begin{diagram}\label{diagram:diagram}
  \begin{equation}
      \begin{tikzcd}[sep=large]
          A \arrow[dd] \arrow[rrr]  & & & B \arrow[dd] \\
          & & &       \\
          C \arrow[rrr]  & & & D   
      \end{tikzcd}    
  \end{equation}
\end{diagram}


\begin{diagram}
  \begin{equation}\label{diagram:diagram1}
      \begin{tikzcd}[sep=large]
          A \arrow[dd] \arrow[rrr]  & & & B \arrow[dd] \\
          & & &       \\
          C \arrow[rrr]  & & & D   
      \end{tikzcd}    
  \end{equation}
\end{diagram}

\begin{equation}\label{eqn:equation}
  y = mx + c
\end{equation}

\Cref{diagram:diagram}
\Cref{diagram:diagram1}
\Cref{eqn:equation}

\end{document}

Result:

enter image description here

Note that I removed the spaces in the \creflabelformat specification, the spaces were showing in the output.

3
  • I did not thought it is this difficult to get what I want :D Suppose I am drawing a diagram in section 1 of chapter 2 then, I would get it as 2.1.1. If I write an equation after that, I will get the equation number as 2.1.2. If I draw another diagram, I would get it as 2.1.3... But, using above label, I am getting as Diagram 2.1 for first diagram and Diagram 2.3 for second diagram... Is there a way to fix it? I tried \def\thediagram{\thechapter. \thesection. \arabic{diagram}}.. It is showing as 2.1.1.1..which is not what I want.. I am extremely sorry for not clarifying it before.. Commented May 21, 2021 at 11:41
  • @PraphullaKoushik \thesection is defined as \thechapter.\arabic{section}, so that already includes the chapter number. So in this case you need \def\thediagram{\thesection.\arabic{diagram}}. Note that this does not work outside of a section, i.e., you start a chapter, first type some text with a diagram, then you start the first section of that chapter. You can either add a check for that in the definition of \thediagram (which is a bit complex to do right), or never use diagrams outside of sections, or change the definition when needed at the start of a new chapter or new section.
    – Marijn
    Commented May 21, 2021 at 12:29
  • Ok. I think I should not have done this. This is becoming complicated. I will follow the usual procedure of writing Diagram \ref{something} and Equation \ref{something}.. Commented May 21, 2021 at 12:40
0
\documentclass[11pt,twoside]{book}

%%% Packages
% 1. fonts
\usepackage[T1]{fontenc} % recommended
%\usepackage[utf8]{inputenc} % no longer needed
\usepackage{fix-cm} % for Computer Modern (comment if using the next line)
%\usepackage{newpxtext,newpxmath} % for Palatino
%\usepackage{upgreek} % do you need it?
%\usepackage{charter} % it has no support for math
%\usepackage{relsize} % do you need it?
\usepackage{a4wide}
% 2. Math
\usepackage{stmaryrd}
\usepackage{mathrsfs}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{mathtools}
\usepackage{bm}% bold math
\usepackage[normalem]{ulem}
% 3. Graphics and special effects
\usepackage{graphicx}
\usepackage{tikz}
\usetikzlibrary{cd}
\usepackage{tikz-cd}
\usepackage{shadow}% do you need it?
\usepackage{fancybox}% do you need it?
\usepackage{scalerel,stackengine}
\usepackage{eso-pic}

% 4. Document formatting
\usepackage{setspace}
\usepackage{fancyhdr}
\usepackage{appendix}
\usepackage[nottoc]{tocbibind}
\usepackage{alltt}
\usepackage{amsthm}


\usepackage[colorlinks]{hyperref}
\usepackage[nameinlink]{cleveref}

\newcounter{diagram}
\newenvironment{diagram}{\setcounter{diagram}{\value{equation}}\refstepcounter{diagram}}{}

\crefname{diagram}{diagram}{diagrams}
\crefname{diagram}{Diagram}{Diagrams}
\creflabelformat{diagram}{(#1) #2 #3}

\begin{document}
    
    \pagenumbering{roman}
    
    \include{Title} 
    
    \newpage
    \thispagestyle{empty}
    \bigskip{}
    \vspace{1cm} \cleardoublepage 
    
    \chapter{chapter 1}
    
    \begin{diagram}\label{diagram:diagram}
        \begin{equation}
            \begin{tikzcd}[sep=large]
                A \arrow[dd] \arrow[rrr]  & & & B \arrow[dd] \\
                & & &       \\
                C \arrow[rrr]  & & & D   
            \end{tikzcd}    
        \end{equation}
    \end{diagram}
    
    
    \begin{diagram}
        \begin{equation}\label{diagram:diagram1}
            \begin{tikzcd}[sep=large]
                A \arrow[dd] \arrow[rrr]  & & & B \arrow[dd] \\
                & & &       \\
                C \arrow[rrr]  & & & D   
            \end{tikzcd}    
        \end{equation}
    \end{diagram}
    
    \begin{equation}\label{eqn:equation}
        y = mx + c
    \end{equation}
    
    \Cref{diagram:diagram}
    \Cref{diagram:diagram1}
    \Cref{eqn:equation}
    

\end{document}

enter image description here

Diagram (reference number) is coming, but, it is not taking chapter/section number. Please see the attached photo.

Is there a way to get chapter/section number?

2
  • you shouldn't bury a question in a answer. It would have much better it you had opened a new question. Commented May 29, 2021 at 14:47
  • beside this: you are only missing \renewcommand\thediagram{\thechapter.\arabic{diagram}}. Commented May 29, 2021 at 14:58
0

Correct if I got anything wrong, because to me the request sounds weird. You want the label to always refer to the number of the diagram, even if you have nested numbered elements?

Because this would simply lead me to \let\refstepcounter\stepcounter within the environment. But I'd still suggest to then create an environment which is already nesting the equation and use \tag so the equation is accessing the diagram counter instead of the equation counter.

Full example would be…

\documentclass[11pt,twoside]{book}

\usepackage[T1]{fontenc} % recommended

\usepackage{amsmath}
\usepackage{tikz-cd}




\usepackage[colorlinks]{hyperref}
\usepackage[nameinlink]{cleveref}

%as posted by Willoughby which I wouldn't do different
% Add cleveref information for diagram
\crefname{diagram}{diagram}{diagrams}
\crefname{diagram}{Diagram}{Diagrams}
\creflabelformat{diagram}{(#1) #2 #3}

\newcounter{diagram}
\newenvironment{diagram}{\setcounter{diagram}{\value{equation}}\refstepcounter{diagram}%
    %let all labels within diagram be set to the diagram counter
    \let\refstepcounter\stepcounter
}{}

\renewcommand{\thediagram}{\thesection.\arabic{diagram}}% or replace \thesection by \thechapter
%or if you don't want to use the chapter
%\renewcommand{\thediagram}{\arabic{section}.\arabic{diagram}}

%add resetcounter to equation

\makeatletter
\@addtoreset{equation}{section}% might be replaced by chapter
\renewcommand{\theequation}{\thesection.\arabic{equation}}% might be replaced by \thechapter
\makeatother

\begin{document}
    \chapter{to have a chapter number}
    \section{to have a section number}
    \begin{diagram}\label{diagram:diagram}
        \begin{equation}
            \begin{tikzcd}[sep=large]
                A \arrow[dd] \arrow[rrr]  & & & B \arrow[dd] \\
                & & &       \\
                C \arrow[rrr]  & & & D   
            \end{tikzcd}    
        \end{equation}
    \end{diagram}
    
    
    \begin{diagram}
        \begin{equation}\label{diagram:diagram1}
            \begin{tikzcd}[sep=large]
                A \arrow[dd] \arrow[rrr]  & & & B \arrow[dd] \\
                & & &       \\
                C \arrow[rrr]  & & & D   
            \end{tikzcd}    
        \end{equation}
    \end{diagram}
    
    \begin{equation}\label{eqn:equation}
        y = mx + c
    \end{equation}
    
    \Cref{diagram:diagram}
    \Cref{diagram:diagram1}
    \Cref{eqn:equation}
    
    \chapter{to have a chapter number}
    \section{to have a section number}
    \begin{diagram}\label{diagram:diagram}
        \begin{equation}
            \begin{tikzcd}[sep=large]
                A \arrow[dd] \arrow[rrr]  & & & B \arrow[dd] \\
                & & &       \\
                C \arrow[rrr]  & & & D   
            \end{tikzcd}    
        \end{equation}
    \end{diagram}
    
    
    
    
\end{document}

updated screenshot including section number

3
  • Thanks for your answer.. Please see my CW answer.. I want to get Diagram 1.1 and not just Diagram 1.. Commented May 29, 2021 at 14:33
  • didn't see that, (the discussion is a bit hard to follow) but that's straightforward then and no big deal, i changed the \theequationand \thediagramand adjusted them to be reset each section. But basically I needed to know if there might be more than one equation inside a diagram environment, because this might lead to a different requirement of the use of the counters.
    – Marei
    Commented May 29, 2021 at 17:31
  • I added the section number as reset, because somewhere upwards i fount that, in the question you posted as an answer yourself you ask for chapter … i guess you're able to replace that yourself in that case.
    – Marei
    Commented May 29, 2021 at 17:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .