fmarotta / kaobook

A LaTeX class for books, reports or theses based on https://github.com/kenohori/thesis and https://github.com/Tufte-LaTeX/tufte-latex.
https://github.com/fmarotta/kaobook
LaTeX Project Public License v1.3c
861 stars 185 forks source link

Getting part name the chapter is in #17

Open doublehourglass opened 5 years ago

doublehourglass commented 5 years ago

I try to get a part name a chapter, section, subsection resides in. So far I only managed to find \p@chapter that adds prefix to the counter. That works, but I cannot make it universal. I mean, I see no way to add another function like the ones from references.sty that could show a reference like: "Chapter B (Some title) on page 33 in Appendix". Is there a way to do that (appendix here is obviously a part)? As a side note - I found that there are no refsubsec and similar functions. I added them on my own as I use subsections quite often, but think they could be generally useful in kaobook as well.

fmarotta commented 5 years ago

This is a good question, thank you. I think this post will be helpful: https://tex.stackexchange.com/questions/312060/how-to-reference-sections-in-other-parts-mentioning-the-part, but note that some more work is required besides copying-and-pasting the code from that answer into the template files.

First of all, that code does not print the name of the part, but only the number. Secondly, it actually works only if the parts are numbered, but in kaobook I tend to use \addpart instead of \part, so my parts are not numbered. I found a quick and dirty solution which you can try by adding the following into the references.sty file. Then, you can use \zlabch and \zrefch to achieve what you want.

\RequirePackage[counter,user,hyperref]{zref}

\newcommand*\parttitle{}

% Redefine \addpart so as to store the part title into the \parttitle command
% NOTE: if you use \part, you should redefine that command in a similar way
\let\oldaddpart\addpart
\renewcommand*{\addpart}[2][]{%
    \ifx\\#1\\% optional argument not present?
        \oldaddpart{#2}%
        \renewcommand*\parttitle{#2}%
    \else
        \oldaddpart[#1]{#2}%
        \renewcommand*\parttitle{#1}%
    \fi 
}

% Create new props for chapter references
\zref@newlist{chapterprop}
\zref@newprop{thepart}[-1]{\thepart}
\zref@newprop{parttitle}[-1]{\parttitle}
\zref@newprop{chapterinfo}[-1]{\number\value{chapter}}

% When labelling a chapter, create both a regular label and a zref label
\newcommand{\zlabch}[1]{%
    \label{ch:#1}%
    \zref@labelbyprops{zch:#1}{thepart,parttitle,chapterinfo,anchor,counter}%
}

% Use the regular label to obtain info about the chapter, and the zref label to obtain info about the part
\newcommand{\zrefch}[1]{%
    % Check first whether the label is defined at all, otherwise 
    % \Countercref etc. would fail!
    \zref@ifrefundefined{zch:#1}{}{%
        % Preexpand some information
        \edef\@tmp@a{\zref@extract{zch:#1}{parttitle}}%
        \edef\@tmp@b{\parttitle}%
        \ifx\@tmp@b\@tmp@a\relax% Compare the part titles
        \frefch{#1} in this \partname%
        \else
        \frefch{#1} in \zref@extract{zch:#1}{parttitle}%
        \fi
    }%  
}

Right now we are about to release a new version of the template to latextemplates.com, so I'd rather not make any changes besides those necessary for the version bump, but right after that I will update the reference.sty package with new zref-based commands. If possible, I would like to have only one command for the labelling (i.e. \labch should work with \zrefch as well), and I hope to find a more elegant way to obtain the \parttitle. I will also add the \refsubsec commands. In the meantime, I hope that the solution I proposed above will work for you. Please let me know about any problems.

doublehourglass commented 5 years ago

Thanks for solution, even if suboptimal.

Nevertheless, I have a feeling that using \p@chapter could be more beneficial, as it can further be regexed in specific functions and arranged accordingly, while being just a single, native vector. It's just a suggestion of a possible way to deal with that issue. I don't much like two labels being attached.

As a side note - with respect to existing reference related functions, they should have an optional parameter that would enable own prefixing in native languages (I mean "Chapter", "Page", etc.).

fmarotta commented 4 years ago

Why don't you like the two labels, if I may ask? The zref package already implements what you needed, and I must admit that I would not know how to use \p@chapter. Besides, are you sure that \p@chapter will also work for, say, sections?

If you managed to write something up, you are welcome to make a pull request; otherwise I think I will go with the zref package.

As for the optional parameter for the language, I totally agree. There already exist some commands, like \figurename or \chaptername, which store language-specific words such as "Figure" or "Chapter", so now I have used these commands whenever possible. The only commands that are still missing are \sectionname, \subsectionname, and the mathematical terms such as \definitionname or \theoremname. I plan to update my style packages with these new keywords in a future release.

doublehourglass commented 4 years ago

I don't like doubling info that is already there. The only issue is that I have no idea how to extract that. The other reason of not liking is mundane - I write a converter of your template to HTML based on Tralics and any additional element adds complexity. When I finish I'll post pull requests with elements I found useful to add.