使用 Linguex/Philex 进行高级交叉引用:跳过和删除

使用 Linguex/Philex 进行高级交叉引用:跳过和删除
\documentclass{report}


\usepackage{philex} % Includes linguex. Only loading it for \phildashes.
\usepackage{cleveref}

% Referring to Subexample Ranges

\crefname{ExNo}{}{}
\crefname{SubExNo}{}{}
\crefname{SubSubExNo}{}{}

\renewcommand{\theExNo}{\arabic{ExNo}}
\renewcommand{\theSubExNo}{\theExNo\arabic{SubExNo}}
\renewcommand{\theSubSubExNo}{\theSubExNo\alph{SubSubExNo}}

\creflabelformat{ExNo}{(#2#1#3)}
\creflabelformat{SubExNo}{(#2#1#3)}
\creflabelformat{SubSubExNo}{(#2#1#3)}

\crefrangelabelformat{ExNo}{(#3#1#4)--(#5#2#6)}
\crefrangelabelformat{SubExNo}{(#3#1#4--#5\crefstripprefix{#1}{#2}#6)} % Skips the common part of the label.
\crefrangelabelformat{SubSubExNo}{(#3#1#4--#5\crefstripprefix{#1}{#2}#6)}
   
\phildashes{.}{}        % Adds a dot between levels 1 and 2 in references (since both have arabic-style labels).
\subformat{1}{}{.}      % Level 2 is of the form “1.”
\subsubformat{a}{}{.}   % Level 3 is of the form “a.”

\begin{document}
    
    \ex. This is the main level.
        \a. This is the sublevel.
            \a. This is the subsublevel. \label{1a}
            \b. Third level too.  \label{1b}
            
I want to keep the \cref{1a} and \crefrange{1a}{1b} styles, but when working on a given main level, I want to be able to make local references to the sublevel and subsublevel, i.e. skip the 1st or 2nd levels : (1a), (1a--b) ; (a), (a)--(b).

\end{document}

在此处输入图片描述

  • 所以我基本上想要定义新的命令,比如说 \localref 和 \localrefrange。

  • 请注意,虽然如果我们使用 Philex 的 \lb 命令而不是 Linguex 的 \ex.,我们也可以根据需要使用 Philex 的 \rfx 和 \rnx 命令跳过第一级,

    • 首先,它们本身对射程没有帮助,

    • 第二,他们只跳过第一级,而不跳过第二级,

      • 即作为对“1.1a”的本地引用,您可以获得“1a”,但绝不会只获得“a”。

不要使用罗马数字因为 \crefstripprefix 不喜欢它。(我们现在有一个涉及xstring包和\mystriptocolon命令的解决方法,参见https://tex.stackexchange.com/a/635636/262813。

  • 我也希望有一个无括号\localref 和 \localrefrange 的变体(比如 \plocalref 和 \plocalrefrange)。

答案1

编辑旧解决方案(参见基础设施一旦你掌握了包(新的之后)就变得毫无用处了zref,参见https://tex.stackexchange.com/a/644037/262813

\documentclass{article}

\usepackage{zref-user}
\usepackage{philorig}

\renewcommand{\label}{\zlabel}

\newcommand{\na}{n\textbackslash a}

\makeatletter

\zref@newprop{theexno}{\arabic{ExNo}}
\zref@newprop{thesubexno}{\arabic{altsub}}
\zref@newprop{theexdepth}{\theExDepth}

\zref@newprop{mainstrip}{\ifnum\theExDepth=3\alph{altsub}.\roman{altsubsub}\else\alph{altsub}\fi}

\zref@newprop{maxstrip}{\ifnum\theExDepth=3\roman{altsubsub}\else{\ifnum\theExDepth=2\alph{altsub}\else\arabic{ExNo}\fi}\fi}


\zref@newprop{mainskip}{\ifnum\theExDepth=1\arabic{ExNo}\else{%
    \ifnum\theExDepth=2\alph{altsub}%
    \else\alph{altsub}.\roman{altsubsub}\fi}%
    \fi}
    
\zref@newprop{maxskip}{\ifnum\theExDepth=3\roman{altsubsub}\else{%
    \ifnum\theExDepth=2\alph{altsub}\else\arabic{ExNo}\fi}%
    \fi}%


\zref@addprops{main}{theexno,thesubexno,theexdepth,mainstrip,maxstrip,mainskip,maxskip}


% Clever Stripping

\newcommand{\pairequal}[2]{%
    \ifnum\arabic{SubExNo}=0(\zref{#1},\,\zref{#2})%
    \else{%
        \ifnum\zref@extract{#1}{theexno}=\zref@extract{#2}{theexno}{%
            \ifnum\zref@extract{#1}{thesubexno}=\zref@extract{#2}{thesubexno}(\zref{#1},\,\zref[maxstrip]{#2})%
            \else (\zref{#1},\,\zref[mainstrip]{#2})\fi}%
        \else (\zref{#1}) and (\zref{#2})\fi}%
    \fi}
            
\newcommand{\rangeequal}[2]{%
    \ifnum\arabic{SubExNo}=0(\zref{#1}--\zref{#2})%
    \else{%
        \ifnum\zref@extract{#1}{theexno}=\zref@extract{#2}{theexno}{%
            \ifnum\zref@extract{#1}{thesubexno}=\zref@extract{#2}{thesubexno}(\zref{#1}--\zref[maxstrip]{#2})%
            \else (\zref{#1}--\zref[mainstrip]{#2})\fi}%
        \else(\zref{#1})--(\zref{#2})\fi}%
    \fi}

\newcommand{\cleverange}[2]{%
    \ifnum\zref@extract{#1}{theexdepth}=\zref@extract{#2}{theexdepth}\rangeequal{#1}{#2}%
    \else(\zref{#1}--\zref{#2})\fi}

\newcommand{\clevepair}[2]{%
    \ifnum\zref@extract{#1}{theexdepth}=\zref@extract{#2}{theexdepth}\pairequal{#1}{#2}%
    \else(\zref{#1},\,\zref{#2})\fi}


% Skipping Variants

% A. Main Skip

\newcommand{\mainskippairequal}[2]{%
    \ifnum\arabic{SubExNo}=0(\zref[mainskip]{#1},\,\zref[mainskip]{#2})%
    \else{%
        \ifnum\zref@extract{#1}{theexno}=\zref@extract{#2}{theexno}{%
            \ifnum\zref@extract{#1}{thesubexno}=\zref@extract{#2}{thesubexno}(\zref[mainskip]{#1},\,\zref[maxstrip]{#2})%
            \else (\zref[mainskip]{#1}) and (\zref[mainstrip]{#2})\fi}%
        \else (\zref[mainskip]{#1},\,\zref[mainskip]{#2})\fi}%
    \fi}
            
\newcommand{\mainskiprangeequal}[2]{%
    \ifnum\arabic{SubExNo}=0(\zref[mainskip]{#1}--\zref[mainskip]{#2})%
    \else{%
        \ifnum\zref@extract{#1}{theexno}=\zref@extract{#2}{theexno}{%
            \ifnum\zref@extract{#1}{thesubexno}=\zref@extract{#2}{thesubexno}(\zref[mainskip]{#1}--\zref[maxstrip]{#2})%
            \else (\zref[mainskip]{#1})--(\zref[mainskip]{#2})\fi}%
        \else (\zref[mainskip]{#1}--\zref[mainskip]{#2})\fi}
    \fi}

\newcommand{\mainskipcleverange}[2]{%
    \ifnum\zref@extract{#1}{theexdepth}=\zref@extract{#2}{theexdepth}\mainskiprangeequal{#1}{#2}%
    \else (\zref[mainskip]{#1}--\zref[mainskip]{#2})\fi}

\newcommand{\mainskipclevepair}[2]{%
    \ifnum\zref@extract{#1}{theexdepth}=\zref@extract{#2}{theexdepth}\mainskippairequal{#1}{#2}%
    \else (\zref[mainskip]{#1},\,\zref[mainskip]{#2})\fi}


% B. Max Skip

\newcommand{\maxskippairequal}[2]{%
    \ifnum\arabic{SubExNo}=0(\zref[maxskip]{#1},\,\zref[maxskip]{#2})%
    \else{%
        \ifnum\zref@extract{#1}{theexno}=\zref@extract{#2}{theexno}%
            {\ifnum\zref@extract{#1}{thesubexno}=\zref@extract{#2}{thesubexno}(\zref[maxskip]{#1}) and (\zref[maxskip]{#2})%
            \else(\zref[maxskip]{#1}) and (\zref[maxskip]{#2})\fi}%
        \else(\zref[maxskip]{#1},\,\zref[maxskip]{#2})\fi}%
    \fi}
            
\newcommand{\maxskiprangeequal}[2]{%
    \ifnum\arabic{SubExNo}=0(\zref[maxskip]{#1}--\zref[maxskip]{#2})%
    \else{%
        \ifnum\zref@extract{#1}{theexno}=\zref@extract{#2}{theexno}{%
            \ifnum\zref@extract{#1}{thesubexno}=\zref@extract{#2}{thesubexno}(\zref[maxskip]{#1})--(\zref[maxskip]{#2})%
            \else(\zref[maxskip]{#1})--(\zref[maxskip]{#2})\fi}%
        \else(\zref[maxskip]{#1}--\zref[maxskip]{#2})\fi}
    \fi}

\newcommand{\maxskipcleverange}[2]{%
    \ifnum\zref@extract{#1}{theexdepth}=\zref@extract{#2}{theexdepth}\maxskiprangeequal{#1}{#2}%
    \else(\zref[maxskip]{#1}--\zref[maxskip]{#2})\fi}

\newcommand{\maxskipclevepair}[2]{%
    \ifnum\zref@extract{#1}{theexdepth}=\zref@extract{#2}{theexdepth}\maxskippairequal{#1}{#2}%
    \else(\zref[maxskip]{#1},\,\zref[maxskip]{#2})\fi}

\makeatother


\begin{document}

\phildashes{}{.} 
\subformat{a}{}{.}     


\lb{main1}{Example One.
    \lba{sub11}{Subexample One-One.
        \lba{subsub111}{Subsubexample One-One-One.}
        \lbb{subsub112}{Subsubexample One-One-Two.}
        \lbz{subsub113}{Subsubexample One-One-Three.}}
    \lbz{sub12}{Subexample One-Two.
        \lba{subsub121}{Subsubexample One-Two-One.}
        \lbz{subsub122}{Subsubexample One-Two-Two.}}}

\lb{main2}{Example Two. 
        \lba{sub21}{Subexample Two-One.}
        \lbz{sub22}{Subexample Two-Two.}}

\vspace{3ex}

\textbf{Clever Ranges} (full - mainskip - maxskip)\vspace{1ex}

\begin{tabular}[c]{lll}

\cleverange{main1}{main2} & \na & \na\\

\cleverange{sub11}{sub12} & \mainskipcleverange{sub11}{sub12} & \na\\

\cleverange{sub11}{sub22} & \na & \na\\

\cleverange{subsub111}{subsub113} & \mainskipcleverange{subsub111}{subsub113} & \maxskipcleverange{subsub111}{subsub113}\\

\cleverange{subsub113}{subsub122} & \mainskipcleverange{subsub113}{subsub122} & \na

\end{tabular}

\vspace{2ex}


------------------\vspace{2ex}

\textbf{Clever Pairs} (full - mainskip - maxskip)\vspace{1ex}

\begin{tabular}[c]{lll}

\clevepair{main1}{main2} & \na & \na\\

\clevepair{sub11}{sub12} & \mainskipclevepair{sub11}{sub12} & \na\\

\clevepair{sub11}{sub22} & \na & \na\\

\clevepair{subsub111}{subsub113} & \mainskipclevepair{subsub111}{subsub113} & \maxskipclevepair{subsub111}{subsub113}\\

\clevepair{subsub113}{subsub122} & \mainskipclevepair{subsub113}{subsub122} & \na

\end{tabular}

\end{document}

在此处输入图片描述


 

旧的解决方案。

我有一个建议不是涉及该cleveref包裹。

  • 正如原帖所指出的,Philex 的 \rfx 和 \rnx 命令经过特意设计,以便能够跳过第一级子标签。

  • Ulrich Diez 找到了跳过第二级的解决方案如果有一个点

  • 下面我使用 Ulrich Diez 的代码进行定义:

    • \rfxx,类似于 Philex 的 \rfx(= 类似于 \ref 但跳过括号之间的 1 级子标签;引用),但也跳过 2 级子标签。

    • \rnxx,类似于 Philex 的 \rnx(= 类似于 \ref 但跳过 1 级子标签;不带括号的引用),但也跳过 2 级子标签。

      \makeatletter
      \@ifdefinable\gobbletodot{\long\def\gobbletodot#1.{}}%
      \newcommand\KeepOnlyStuffBehindLastDot[1]{%
        \ifcat$\detokenize\expandafter{\gobbletodot#1.}$%
        \expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi
        {#1}{\expandafter\KeepOnlyStuffBehindLastDot\expandafter{\gobbletodot#1}}%
      }%
      \@ifdefinable\rfxx{%
        \DeclareRobustCommand\rfxx[1]{%
          \IfRefUndefinedBabel{#1}{\refused{#1}\nfss@text{\reset@font\bfseries??}}%
                              {%
                                \@ifundefined{hyperref}{\@firstofone}{\hyperref[{#1}]}%
                                {%
                                  (\expandafter\expandafter\expandafter\KeepOnlyStuffBehindLastDot
                                  \expandafter\expandafter\expandafter{\getrefbykeydefault{#1}{}{??}})%
                                }%
                              }%
        }%
      }%
      
      \@ifdefinable\rnxx{%
        \DeclareRobustCommand\rnxx[1]{%
          \IfRefUndefinedBabel{#1}{\refused{#1}\nfss@text{\reset@font\bfseries??}}%
                              {%
                                \@ifundefined{hyperref}{\@firstofone}{\hyperref[{#1}]}%
                                {%
                                  \expandafter\expandafter\expandafter\KeepOnlyStuffBehindLastDot
                                  \expandafter\expandafter\expandafter{\getrefbykeydefault{#1}{}{??}}%
                                }%
                              }%
        }%
      }%
      
      \makeatother
      
  • 现在我定义范围:

    • 使用 Philex 的命令,我们可以跳过第一级子标签:

      \newcommand{\rfxrange}[2]{\rfx{#1}--\rfx{#2}}
      \newcommand{\rnxrange}[2]{\rnx{#1}--\rnx{#2}}
      
    • 使用 Ulrich Diez 代码中的新命令,我们也可以跳过第二级:

      \newcommand{\rfxxrange}[2]{\rfxx{#1}--\rfxx{#2}}
      \newcommand{\rnxxrange}[2]{\rnxx{#1}--\rnxx{#2}}
      
  • 然后我添加“前缀剥离”的可能性:

    • 下面的几行合并了左边的部分包括具有右部分的 2 级子标签跳过它。

      \newcommand{\rfxstrip}[2]{(\rnx{#1}--\rnxx{#2})}
      \newcommand{\rnxstrip}[2]{\rnx{#1}--\rnxx{#2}}
      

现在原始 MWE 中的应用程序:

\documentclass{report}

\usepackage[hidelinks]{hyperref}
\usepackage{refcount}
\usepackage{philex} % Includes linguex. Only loading it for \phildashes.
\usepackage{cleveref}

% Referring to Subexample Ranges

\crefname{ExNo}{}{}
\crefname{SubExNo}{}{}
\crefname{SubSubExNo}{}{}

\renewcommand{\theExNo}{\arabic{ExNo}}
\renewcommand{\theSubExNo}{\theExNo\arabic{SubExNo}}
\renewcommand{\theSubSubExNo}{\theSubExNo\alph{SubSubExNo}}

\creflabelformat{ExNo}{(#2#1#3)}
\creflabelformat{SubExNo}{(#2#1#3)}
\creflabelformat{SubSubExNo}{(#2#1#3)}

\crefrangelabelformat{ExNo}{(#3#1#4)--(#5#2#6)}
\crefrangelabelformat{SubExNo}{(#3#1#4--#5\crefstripprefix{#1}{#2}#6)} % Skips the common part of the label.
\crefrangelabelformat{SubSubExNo}{(#3#1#4--#5\crefstripprefix{#1}{#2}#6)}
   
\phildashes{.}{.}        % Adds a dot between levels in references
\subformat{1}{}{.}      % Level 2 is of the form “1.”
\subsubformat{a}{}{.}   % Level 3 is of the form “a.”




% Skip the first and second levels
\makeatletter
\@ifdefinable\gobbletodot{\long\def\gobbletodot#1.{}}%
\newcommand\KeepOnlyStuffBehindLastDot[1]{%
  \ifcat$\detokenize\expandafter{\gobbletodot#1.}$%
  \expandafter\@firstoftwo\else\expandafter\@secondoftwo\fi
  {#1}{\expandafter\KeepOnlyStuffBehindLastDot\expandafter{\gobbletodot#1}}%
}%
\@ifdefinable\rfxx{%
  \DeclareRobustCommand\rfxx[1]{%
    \IfRefUndefinedBabel{#1}{\refused{#1}\nfss@text{\reset@font\bfseries??}}%
                        {%
                          \@ifundefined{hyperref}{\@firstofone}{\hyperref[{#1}]}%
                          {%
                            (\expandafter\expandafter\expandafter\KeepOnlyStuffBehindLastDot
                            \expandafter\expandafter\expandafter{\getrefbykeydefault{#1}{}{??}})%
                          }%
                        }%
  }%
}%

\@ifdefinable\rnxx{%
  \DeclareRobustCommand\rnxx[1]{%
    \IfRefUndefinedBabel{#1}{\refused{#1}\nfss@text{\reset@font\bfseries??}}%
                        {%
                          \@ifundefined{hyperref}{\@firstofone}{\hyperref[{#1}]}%
                          {%
                            \expandafter\expandafter\expandafter\KeepOnlyStuffBehindLastDot
                            \expandafter\expandafter\expandafter{\getrefbykeydefault{#1}{}{??}}%
                          }%
                        }%
  }%
}%


\makeatother



%Now I define ranges :

\newcommand{\rfxrange}[2]{\rfx{#1}--\rfx{#2}}
\newcommand{\rnxrange}[2]{\rnx{#1}--\rnx{#2}}

\newcommand{\rfxxrange}[2]{\rfxx{#1}--\rfxx{#2}}
\newcommand{\rnxxrange}[2]{\rnxx{#1}--\rnxx{#2}}

\newcommand{\rfxstrip}[2]{(\rnx{#1}--\rnxx{#2})}
\newcommand{\rnxstrip}[2]{\rnx{#1}--\rnxx{#2}}



\begin{document}
    
    \lb{main}{This is the main level.
        \lba{sub}{This is the sublevel.
            \lba{subsub-a}{This is the subsublevel.}
            \lbz{subsub-b}{Third level too.}}}
            


  I want to keep the \cref{sub} and \crefrange{subsub-a}{subsub-b} styles, but when working on a given main level, I want to be able to make local references to the sublevel and subsublevel, i.e. skip the 1st or 2nd levels.
    
   I can use \textbackslash rfxrange and \textbackslash rnxrange commands to skip 1 level as in \rfxrange{subsub-a}{subsub-b} or \rnxrange{subsub-a}{subsub-b}
    
    and \textbackslash rfxxrange and \textbackslash rnxxrange commands to skip the second level as well, as in \rfxxrange{subsub-a}{subsub-b} or \rnxxrange{subsub-a}{subsub-b}.
    
    I can also, while skipping the first level, use my new \textbackslash rfxstrip and \textbackslash rnxstrip commands so as to obtain a \textbackslash crefstripprefix effect :
    
    \rfxstrip{subsub-a}{subsub-b} and \rnxstrip{subsub-a}{subsub-b}.
    
    \end{document}

在此处输入图片描述

  • 现在为了使命令名称与cleveref非本地范围协调,我们可以定义:

    \newcommand{\rfrange}[2]{(\cref{#1})--(\cref{#2})}
    \newcommand{\rnrange}[2]{\cref{#1}--\cref{#2}}
    
    \newcommand{\rfstrip}[2]{(\crefrange{#1}{#2})}
    \newcommand{\rnstrip}[2]{\crefrange{#1}{#2}}
    

在此处输入图片描述

另请参阅:

参考范围中的交叉引用 [(1a-1e) 至 (1a-e)]

有什么方法可以引用标签的特定组件吗?

参考一系列子示例

相关内容