4

MWE

\documentclass{book}
\begin{document}

\part{ONE}
\chapter{One}
a\label{sec:a}

\section{1-One}
c \pageref{sec:e} [please see Part II page 3] %< what I'm trying to write

d \pageref{sec:a} [please see page 3] %< what I'm trying to write

\newpage\setcounter{page}{1}

\part{TWO}
\chapter{One}
e\label{sec:e}

\section{1-One}
f \pageref{sec:e} [please see page 3] %< what I'm trying to write

g \pageref{sec:a} [please see Part I page 3] %< what I'm trying to write

\end{document}

If the reference is in same part, it will only write the page number. If the reference is in the other part, it will write the part and page number.

  • Have you seen tex.stackexchange.com/q/314279/107497 ? – Teepeemm Sep 3 at 15:54
  • @Teepeemm, It worked very well. That's really what I was looking for. How can I give you a bounty? If you want, you can fix the shortcomings in my solution. So the bounty goes to you. – Özgür Sep 4 at 11:07
  • I appreciate the sentiment, but posting a link doesn't really seem like it's worth me getting a bounty. You've put more effort into your answer than I have. – Teepeemm Sep 9 at 12:46
2

MWE

\documentclass[english]{book}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}

%\setcounter{secnumdepth}{5} % fünf Ebenen

\usepackage{zref-user,zref-counter}
\usepackage{expl3}
\makeatletter
\zref@newprop{chapter}[1]{\the\c@chapter}
\zref@newprop{part}[1]{\the\c@part}
\zref@newprop{section}[1]{\the\c@section}
\zref@newprop{subsection}[1]{\the\c@subsection}
\zref@addprop{main}{chapter}
\zref@addprop{main}{part}
\zref@addprop{main}{section}
\zref@addprop{main}{subsection}
\ExplSyntaxOn

\newcommand\myref[1]{%
    %\hyperref[#1]{
        \int_compare:nTF
        {
            \zref@extractdefault {#1} {part} {0} = \the\c@part
        }
        {
            %ref is from current section
            \int_compare:nTF
            {\zref@extractdefault {#1} {part} {0}  = 0 }
            {Was~nun?}
            {
[Please\ see\ page\ \pageref{#1}]
}
        }
        {
            %ref is from other section
[please\ see\ Part\ \int_to_Roman:n {\zref@extractdefault {#1} {part} {0}}\
 Chapter\ \int_to_Roman:n {\zref@extractdefault {#1} {chapter} {0}}\
Section\ \int_to_Roman:n {\zref@extractdefault {#1} {section} {0}}\ page\ \pageref{#1}]
        }}%}
        \ExplSyntaxOff
        \makeatother
        \newcommand\Label[1]{\zlabel{#1}\label{#1}}

        \begin{document}

\part{ONE}
\chapter{One}
\Label{part:a}
\section{1-One}
\myref{part:secondofsecond}

\myref{part:a}
\newpage\setcounter{page}{1}

\part{TWO}
\chapter{One}
\Label{part:e}
\section{1-One}
\myref{part:a}
\section{First}
\myref{part:e}
\section{Second}
\subsection{Firstofsecond}
\myref{part:a}
\subsection{Secondofsecond}
\Label{part:secondofsecond}

\end{document}

I changed this example a little, https://tex.stackexchange.com/a/325319/3307h5. But there is extra space as shown in the pictures below:

enter image description here

enter image description here

I have no problem other than this problem. This code works for me. Of course I changed Ulrike Fischer's solution. I can't imagine what will happen in the future.

  • It's not so much that there's extra space as that the section number isn't appearing. Which might make sense, as \label{part:a} isn't inside a section. You're forcing that space with your \ commands, but without the section number you get two in a row. – Teepeemm Sep 9 at 12:50
  • For second cross-reference, what you want exactly? [please see Part I Chapter I page 3] or [please see Part I Chapter I Section I page 3]? you have two way: first make another label inside the section of first chapter and then call it. second, you can add a long code to check that is there any section in the label place! – C.F.G Sep 10 at 6:07
1

I hope this helps:

\documentclass{book}
\begin{document}

\part{ONE} \label{part:1}

\chapter{One}\label{part:1:ch:1}

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et.

\section{1-One}

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et, please see Part \ref{part:1}, Chapter \ref{part:1:ch:1}

\newpage
\setcounter{page}{1}        % Page Number to 1
\setcounter{chapter}{0}     % Next Chapter Number to 1

\part{TWO} \label{part:2} 

\chapter{One}\label{part:2:ch:1}

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et, please see Part \ref{part:1}, Chapter \ref{part:1:ch:1}.

\section{1-One}

Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et, please see Part \ref{part:1}, Chapter \ref{part:1:ch:1} and Part \ref{part:2}, Chapter \ref{part:2:ch:1}.


\end{document}

which produces:

enter image description here

  • @Özgür I am sorry, I did not read your question properly. I will still leave the answer here as a bad example. – Hiwi Makro Aug 31 at 10:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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