• Tidak ada hasil yang ditemukan

Sorting and comparison of counters

Dalam dokumen The cleveref package - Index of / (Halaman 41-47)

12.2 Utility Macros

12.2.4 Sorting and comparison of counters

645 \else%

646 \edef\@tempa{\cref@stack@top{#4}}%

647 \expandafter\@cmp\expandafter{\@tempa}{#2}{\cref@result}%

648 \ifnum\cref@result=2\relax%

649 \cref@stack@push{#1,#2#3}{#4}%

650 \else%

651 \cref@stack@pop{#4}%

652 \edef\@tempa{{\noexpand#1,\@tempa}{\noexpand#2}%

653 {\noexpand#3}{\noexpand#4}}%

654 \expandafter\def\expandafter\@iterate\expandafter%

655 {\expandafter\@cref@stack@insert\@tempa}%

656 \fi%

657 \fi%

658 \@iterate}

680 \cref@getprefix{#2}{\@prefixb}%

681 \cref@stack@init{\@countstacka}%

682 \expandafter\cref@stack@push\expandafter%

683 {\@countera}{\@countstacka}%

684 \ifx\@prefixa\@empty\else%

685 \expandafter\cref@stack@push\expandafter%

686 {\@prefixa}{\@countstacka}%

687 \fi%

688 \cref@stack@init{\@countstackb}%

689 \expandafter\cref@stack@push\expandafter%

690 {\@counterb}{\@countstackb}%

691 \ifx\@prefixb\@empty\else%

692 \expandafter\cref@stack@push\expandafter%

693 {\@prefixb}{\@countstackb}%

694 \fi%

695 \@cref@countercmp%

696 \fi%

697 \fi%

698 \fi%

699 \fi%

700 \expandafter\endgroup\expandafter%

701 \chardef\expandafter#3\expandafter=\cref@result\relax}

\cref@countercmp The\@cref@countercmpmacro recursively compares counter components until it runs out of components for one of the references, or finds an two corresponding components that are unequel.

702\def\@cref@countercmp{%

703 \let\@iterate\relax%

704 \cref@isstackempty{\@countstacka}%

705 \if@cref@stackempty%

706 \cref@isstackempty{\@countstackb}%

707 \if@cref@stackempty%

708 \def\cref@result{0}%

709 \else%

710 \def\cref@result{1}%

711 \fi%

712 \else%

713 \cref@isstackempty{\@countstackb}%

714 \if@cref@stackempty%

715 \def\cref@result{2}%

716 \else%

717 \edef\@tempa{\cref@stack@top{\@countstacka}}%

718 \cref@stack@pop{\@countstacka}%

719 \edef\@tempb{\cref@stack@top{\@countstackb}}%

720 \cref@stack@pop{\@countstackb}%

721 \ifnum\@tempa<\@tempb\relax%

722 \def\cref@result{1}%

723 \else%

724 \ifnum\@tempa>\@tempb\relax%

725 \def\cref@result{2}%

726 \else%

727 \def\@iterate{\@cref@countercmp}%

728 \fi%

729 \fi%

730 \fi%

731 \fi%

732 \@iterate}

\if@cref@inresetlist

\cref@isinresetlist

\cref@resetby

We need to be able to determine which counter is used to reset a given counter.

Usually, resets are done by sectioning counters, and we assume that to be the case here. \cref@isinresetlistsearches through one counter’s reset list, stored in\cl@hcounteri, to determine whether another counter appears there, and sets the new conditional appropriately. \cref@reset@bysearches through all the sec- tioning counters’ reset lists, from lowest-level (subsubsection) to highest (part), checking whether the given counter is in the list, and returns the first sectioning counter in whose list it appears. (The value is returned by defining its second argument, which should be a macro name.)

733\newif\if@cref@inresetlist

734\def\cref@isinresetlist#1#2{%

735 \begingroup%

736 \def\@counter{#1}%

We locally redefine \@elt, which appears at the head of the expansion of

\cl@hcounteri, so that entries in the reset list end up separated by commas, thus can be treated as a stack.

737 \def\@elt##1{##1,}%

738 \expandafter\ifx\csname cl@#2\endcsname\relax%

739 \def\cref@resetstack{,\@nil}%

740 \else%

741 \edef\cref@resetstack{\csname cl@#2\endcsname\noexpand\@nil}%

742 \fi%

743 \let\@nextcounter\relax%

744 \cref@isstackfull{\cref@resetstack}%

745 \@whilesw\if@cref@stackfull\fi{%

746 \edef\@nextcounter{\cref@stack@top{\cref@resetstack}}%

747 \ifx\@nextcounter\@counter%

748 \@cref@stackfullfalse%

749 \else%

750 \let\@nextcounter\relax%

751 \cref@stack@pop{\cref@resetstack}%

752 \cref@isstackfull{\cref@resetstack}%

753 \fi}%

754 \ifx\@nextcounter\relax%

755 \def\@next{\@cref@inresetlistfalse}%

756 \else%

757 \def\@next{\@cref@inresetlisttrue}%

758 \fi%

759 \expandafter%

760 \endgroup%

761 \@next}

FIXME: We could easily remove the hard-coded search order in\cref@resetby and, say, replace it with a customisable list of counters to search in order. But, so far, I’ve yet to encounter a need for anything other than the hard-coded default.

762\def\cref@resetby#1#2{%

763 \let#2\relax%

If counter in question issubfigureor subtable, check if it’s reset byfigure or table, respectively.

764 \def\@tempa{#1}%

765 \def\@tempb{subfigure}%

766 \ifx\@tempa\@tempb%

767 \cref@isinresetlist{#1}{figure}%

768 \if@cref@inresetlist%

769 \def#2{figure}%

770 \fi%

771 \fi%

772 \def\@tempa{#1}%

773 \def\@tempb{subtable}%

774 \ifx\@tempa\@tempb%

775 \cref@isinresetlist{#1}{table}%

776 \if@cref@inresetlist%

777 \def#2{table}%

778 \fi%

779 \fi%

If counter in question isequation, and the counter parentequation is defined, check if it’s reset by that. The parentequation counter is used by amsmath’s subequationsenvironment. Althoughamsmathdoesn’t implementsubequations using counter reset lists,cleveref’samsmathsupport (above) tweaks the reset lists insidesubequationsenvironments to hook into this mechanism. We should really only check this whenamsmathis loaded, but checking it anyway might catch other packages that independently implement amsmath’s subequations environment (are there any?).

780 \@ifundefined{cl@parentequation}{}{%

781 \def\@tempa{#1}%

782 \def\@tempb{equation}%

783 \ifx\@tempa\@tempb%

784 \cref@isinresetlist{#1}{parentequation}%

785 \if@cref@inresetlist%

786 \expandafter\ifnum\c@parentequation=0\else%

787 \def#2{parentequation}%

788 \fi%

789 \fi%

790 \fi}%

If counter in question isenumhxi, check if it’s reset by a higher-levelenumhxi.

791 \def\@tempa{#1}%

792 \def\@tempb{enumii}%

793 \ifx\@tempa\@tempb%

794 \def#2{enum}%

795 \fi%

796 \def\@tempb{enumiii}%

797 \ifx\@tempa\@tempb%

798 \def#2{enum}%

799 \fi%

800 \def\@tempb{enumiv}%

801 \ifx\@tempa\@tempb%

802 \def#2{enum}%

803 \fi%

804 \def\@tempb{enumv}%

805 \ifx\@tempa\@tempb%

806 \def#2{enum}%

807 \fi%

808 \def\@tempb{enum}%

809 \ifx#2\@tempb%

810 \cref@isinresetlist{#1}{enumiv}%

811 \if@cref@inresetlist%

812 \def#2{enumiv}%

813 \else%

814 \cref@isinresetlist{#1}{enumiii}%

815 \if@cref@inresetlist%

816 \def#2{enumiii}%

817 \else%

818 \cref@isinresetlist{#1}{enumii}%

819 \if@cref@inresetlist%

820 \def#2{enumii}%

821 \else%

822 \cref@isinresetlist{#1}{enumi}%

823 \if@cref@inresetlist%

824 \def#2{enumi}%

825 \else%

826 \cref@isinresetlist{#1}{part}%

827 \if@cref@inresetlist%

828 \def#2{part}%

829 \else%

830 \let#2\relax%

831 \fi%

832 \fi%

833 \fi%

834 \fi%

835 \fi%

836 \fi%

If we haven’t found anything so far, check if it’s reset by a sectioning command.

837 \ifx#2\relax%

838 \cref@isinresetlist{#1}{table}%

839 \if@cref@inresetlist%

840 \def#2{table}%

841 \else%

842 \cref@isinresetlist{#1}{subsubsection}%

843 \if@cref@inresetlist%

844 \def#2{subsubsection}%

845 \else%

846 \cref@isinresetlist{#1}{subsection}%

847 \if@cref@inresetlist%

848 \def#2{subsection}%

849 \else%

850 \cref@isinresetlist{#1}{section}%

851 \if@cref@inresetlist%

852 \def#2{section}%

853 \else%

854 \cref@isinresetlist{#1}{chapter}%

855 \if@cref@inresetlist%

856 \def#2{chapter}%

857 \else%

858 \cref@isinresetlist{#1}{part}%

859 \if@cref@inresetlist%

860 \def#2{part}%

861 \else%

862 \let#2\relax%

863 \fi%

864 \fi%

865 \fi%

866 \fi%

867 \fi%

868 \fi%

869 \fi}

\if@cref@refconsecutive

\cref@isrefconsecutive

Define a new conditional to test whether two references are consecutive (needed when type-setting reference ranges). This uses the counter and prefix (i.e. format- ted version of the counter that resets the label’s counter) information provided by

\r@cref@hlabeli(via the aux file) to check if the prefices are identical (i.e. the references come from the same chapter, section or whatever), and that the label counters differ by 0 or 1.

870\newif\if@cref@refconsecutive%

871\def\cref@isrefconsecutive#1#2{%

872 \begingroup%

873 \countdef\refa@counter=0%

874 \countdef\refb@counter=1%

875 \cref@getcounter{#1}{\cref@result}%

876 \refa@counter=\cref@result%

877 \cref@getcounter{#2}{\cref@result}%

878 \refb@counter=\cref@result%

879 \cref@getprefix{#1}{\refa@prefix}%

880 \cref@getprefix{#2}{\refb@prefix}%

881 \def\@after{\@cref@refconsecutivefalse}%

882 \ifx\refa@prefix\refb@prefix%

883 \ifnum\refa@counter=\refb@counter\relax%

884 \def\@after{\@cref@refconsecutivetrue}%

885 \else%

886 \advance\refa@counter 1\relax%

887 \ifnum\refa@counter=\refb@counter\relax%

888 \def\@after{\@cref@refconsecutivetrue}%

889 \fi%

890 \fi%

891 \fi%

892 \expandafter\endgroup\@after}

Dalam dokumen The cleveref package - Index of / (Halaman 41-47)

Dokumen terkait