具有跟踪更改和换行功能的 URL

具有跟踪更改和换行功能的 URL

我正在尝试处理 URL,以便能够:

  • 如果 URL 太长超出页面宽度,则将其拆分为多行
  • 包括轨道变化模式,这意味着能够:
    • 删除线和下划线标记 URL 文本,以及
    • 改变文本颜色

为了做到这一点,我\bignumber{123456....}从 Michael Ummels 在将非常大的数字拆分成多行并对其进行了一些修改,以便我在 URL 文本中引入两个宏:\added{}和,\deleted{}它们控制跟踪更改模式的文本格式。这些宏依次使用宏\bignumber\bignumberAdded{}\bignumberDeleted{})来确保跟踪更改模式中提供的文本也被拆分为多行。

这是代码,其中包括一系列示例来展示代码的作用。

\documentclass[a4paper,10pt]{article}

% ---------------------------------------------------------
% LOAD USER-SPECIFIC PACKAGES
% ---------------------------------------------------------
\RequirePackage[top=1in, bottom=1in, left=0.7in, right=0.7in]{geometry}
\RequirePackage[parfill]{parskip} % Non-zero paragraph
\RequirePackage{fontspec}         % Specify system fonts
\RequirePackage{xstring}        % Used for string manipulation, such as IfEqCase
\RequirePackage{soul}           % Used to underline and strike out
\RequirePackage{amsmath}        % Used to enable deleted and added inside math formulas
\RequirePackage{lipsum}

\RequirePackage[
    colorlinks=true,
    citecolor=esadeepspace,
    urlcolor=blue,
    linkcolor=black]{hyperref} 

% ------------------------------------------------------------------------------
% TRACK CHANGES TYPESETTING
% ------------------------------------------------------------------------------
%%% redlining of text
 
%the bignumber function is an iterative function to enable the split long numbers or long strings in multiple lines at any point of their body.
%this is however a re-work of original bignumber implementation to enable to put inside \deleted{} or \added{} strings
\newcounter{AddedDeletedLength}\setcounter{AddedDeletedLength}{0}
\newcounter{FLAGbignumber}\setcounter{FLAGbignumber}{0}
\makeatletter
\def\@bignumber#1#2{%
    %searching if there is any \added{} string wihin the href text
    \ifx#1\added{}%
    %print the found \added{}
    \added{#2}%\let\next\@bignumber%%%
    \setcounter{FLAGbignumber}{1}%
    \fi%
    %searching if there is any \deleted{} string wihin the href text
    \ifx#1\deleted{}%
    %print the found \deleted{}
    \deleted{#2}%\let\next\@bignumber%%%
    \setcounter{FLAGbignumber}{1}%
    \fi%
    %if any \added{} or \deleted{} is found, the length of their string is computed and set in counter
    \ifnum\value{FLAGbignumber}=1%
        \def\AddedDeletedString{#2}%
        \StrLen{\AddedDeletedString}[\tempstring]%
        \setcounter{AddedDeletedLength}{\tempstring}%%
    \fi%
    %enter here only when #1 is not \added or \deleted
    \ifnum\value{FLAGbignumber}=0%
            %enter here if #2 is an end line to split the bignumber in multiple lines
            \ifx#2\end%
                    %print #1 if only it is not part of \added{} or \deleted{}
                    \ifnum\value{AddedDeletedLength}<0%
                    #1%
                    \fi%
                    \ifnum\value{AddedDeletedLength}=0%
                    #1%
                    \fi%
            %enter here if #2 is NOT an end line, and add some glue to let the split in multiple lines
            \else%
                    %print #1 if only it is not part of \added{} or \deleted{}
                    \ifnum\value{AddedDeletedLength}<0%
                    #1\hspace{0pt plus 1pt}%
                    \fi%
                    \ifnum\value{AddedDeletedLength}=0%
                    #1\hspace{0pt plus 1pt}%
                    \fi%
            \fi%
    \fi%
            \ifx#2\end%
                \let\next\@gobble%
            %enter here if #2 is NOT an end line, and add some glue to let the split in multiple lines
            \else%
                \let\next\@bignumber%
            \fi%
    \ifnum\value{FLAGbignumber}=0%
        %Updating the length of AddedDeletedLength so to compute when #1 is no more part of \added{} or \deleted{}
        \def\AddedDeletedStringNEW{#1}%
        \StrLen{\AddedDeletedStringNEW}[\tempstring]%
        \setcounter{AddedDeletedLength}{\numexpr\value{AddedDeletedLength}-\tempstring\relax}%
    \fi%
    %FLAGbignumber is activated only once, at the beginning when \added{} or \deleted{} is encounter, in order to compute its length
    %othewise it is always zero
    \setcounter{FLAGbignumber}{0}%
    \next#2}%
\newcommand{\bignumber}[1]{\setcounter{AddedDeletedLength}{0}%
\def\temp{#1}\ifx\temp\empty\else\@bignumber#1\end\fi}
\makeatother



%This is a rework of the orginal bignumber function to make the string to appear with the \added{} font
\makeatletter
\def\@bignumberDeleted#1#2{%
  \ifx#2\end%
    \textcolor{red}{\st{#1}}\let\next\@gobble%
  \else%
    \textcolor{red}{\st{#1}}\hspace{0pt plus 1pt}\let\next\@bignumberDeleted%
  \fi%
  \next#2}
\newcommand{\bignumberDeleted}[1]{\def\temp{#1}\ifx\temp\empty\else\@bignumberDeleted#1\end\fi}
\makeatother

%This is a rework of the orginal bignumber function to make the string to appear with the \deleted{} font
\makeatletter
\def\@bignumberAdded#1#2{%
  \ifx#2\end%
    \textcolor{green}{\ul{#1}}\let\next\@gobble%
  \else%
    \textcolor{green}{\ul{#1}}\hspace{0pt plus 1pt}\let\next\@bignumberAdded%
  \fi%
  \next#2}
\newcommand{\bignumberAdded}[1]{\def\temp{#1}\ifx\temp\empty\else\@bignumberAdded#1\end\fi}
\makeatother

 
%%This is the newly created \href macro to handle my problem
\newcounter{hrefFLAG}\setcounter{hrefFLAG}{0}
\newcommand{\hrefMy}[2]{\setcounter{hrefFLAG}{1}\href{#1}{\bignumber{#2}}\setcounter{hrefFLAG}{0}}

%%This is a re-work of the original \deleted implementation to be able to handle also url using the \hrefMy function
\setstcolor{red}
\DeclareRobustCommand{\deleted}[1]{%
  \ifmmode\text{{\textcolor{red}{\texorpdfstring{\st{$#1$}}{}}}}\else{%
  \ifnum \value{hrefFLAG}>0%
    \bignumberDeleted{#1}%
  \else%
  \textcolor{red}{\texorpdfstring{\st{#1}}{}}%
  \fi}\fi} 


%This is a re-work of the original \added implementation to be able to handle also url using the \hrefMy function
\setulcolor{green}
\DeclareRobustCommand{\added}[1]{%
  \ifmmode\text{{\textcolor{green}{\texorpdfstring{\ul{$#1$}}{}}}}\else{%
  \ifnum \value{hrefFLAG}>0%
    \bignumberAdded{#1}%
  \else%
  \textcolor{green}{\texorpdfstring{\ul{#1}}{}}%
  \fi}\fi}



%=========================================================
\begin{document}

  
\textbf{Example of added plein text:}

\added{Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit, vestibulum ut, placerat ac, adipiscing vitae,
felis. Curabitur dictum gravida mauris. Nam arcu libero, nonummy eget, consectetuer id, vulputate a, magna.
Donec vehicula augue eu neque. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac
turpis egestas. Mauris ut leo. Cras viverra metus rhoncus sem. Nulla et lectus vestibulum urna fringilla ultrices.}

\textbf{Example of deleted plein text:}

\deleted{Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Ut purus elit, vestibulum ut, placerat ac, adipiscing vitae,
felis. Curabitur dictum gravida mauris. Nam arcu libero, nonummy eget, consectetuer id, vulputate a, magna.
Donec vehicula augue eu neque. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac
turpis egestas. Mauris ut leo. Cras viverra metus rhoncus sem. Nulla et lectus vestibulum urna fringilla ultrices.}


\textbf{Example of using the hrefMy\{\} macro with track changes by adding and deleting a very long URL:}

\hrefMy{https://www.google.com/}{very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url\added{very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url}\deleted{very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url}}

\textbf{Another example of using the hrefMy\{\} macro with track changes:}

\hrefMy{https://www.google.com/}{very-long-url-\deleted{veeery-long-url-}\added{very-long-url-}very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url}


\end{document}

在此处输入图片描述

到目前为止一切顺利,它似乎运行良好。现在的问题是,当我启动添加或删除的 url 文本时,它会给我一个非常奇怪的错误同一封信这很奇怪。换句话说,使用一个非常简单的示例,它可以工作:

%=========================================================
\begin{document}

\hrefMy{https://www.google.com/}{abcdef}

\hrefMy{https://www.google.com/}{\added{abcdef}}

\hrefMy{https://www.google.com/}{\deleted{abcdef}}

\end{document}

这也有效(请注意,字符串现在以两个连续的“a”开头):

%=========================================================
\begin{document}

\hrefMy{https://www.google.com/}{aabcdef}

\end{document}

但这不起作用:

%=========================================================
\begin{document}

\hrefMy{https://www.google.com/}{\added{aabcdef}}

\hrefMy{https://www.google.com/}{\deleted{aabcdef}}

\end{document}

由于文本以“aa”开头,即两个连续的相同字符。Latex 给出了以下错误:

! Missing \endcsname inserted.
<to be read again>
\let
l.203 ...https://www.google.com/}{\added{aabcdef}}
The control sequence marked <to be read again> should
not appear between \csname and \endcsname.

有什么帮助吗?代码怎么可能不能处理具有两个连续相同字母的字符串?

谢谢!

PS 我无法使用其他宏作为 sloppypar、seqsplit 甚至通用函数,因为它们与我用于删除线( )和下划线( )\url{}的 soul 包不兼容。\st{}\ul{}

答案1

感谢 Ulrike 的提示,我找到了解决自己问题的方法。我在此处发布了更新后的代码,希望它对其他人有用。如果您测试后发现其他错误,请记下来!

\documentclass[a4paper,10pt]{article}

% ---------------------------------------------------------
% LOAD USER-SPECIFIC PACKAGES
% ---------------------------------------------------------
\RequirePackage[top=1in, bottom=1in, left=0.7in, right=0.7in]{geometry}
\RequirePackage[parfill]{parskip} % Non-zero paragraph
\RequirePackage{fontspec}         % Specify system fonts
\RequirePackage{xstring}        % Used for string manipulation, such as IfEqCase
\RequirePackage{soul}           % Used to underline and strike out
\RequirePackage{amsmath}        % Used to enable deleted and added inside math formulas


\RequirePackage[
    colorlinks=true,
    citecolor=esadeepspace,
    urlcolor=blue,
    linkcolor=black]{hyperref} 


% ------------------------------------------------------------------------------
% TRACK CHANGES TYPESETTING
% ------------------------------------------------------------------------------
%%% redlining of text
 
%the bignumber function is an iterative function to enable the split long numbers or long strings in multiple lines at any point of their body.
%this is however a re-work of original bignumber implementation to enable to put inside \deleted{} or \added{} strings
\newcounter{AddedDeletedLength}\setcounter{AddedDeletedLength}{0}
\newcounter{FLAGbignumber}\setcounter{FLAGbignumber}{0}
\makeatletter
\def\@bignumber#1#2{%
    %searching if there is any \added{} string wihin the href text
    \ifx#1\added{}%
    %print the found \added{}
    \bignumberAdded{#2}%%
    \setcounter{FLAGbignumber}{1}%
    \fi%
    %searching if there is any \deleted{} string wihin the href text
    \ifx#1\deleted{}%
    %print the found \deleted{}
    \bignumberDeleted{#2}%%
    \setcounter{FLAGbignumber}{1}%
    \fi%
    %if any \added{} or \deleted{} is found, the length of their string is computed and set in counter
    \ifnum\value{FLAGbignumber}=1%
        \def\AddedDeletedString{#2}%
        \StrLen{\AddedDeletedString}[\tempstring]%
        \setcounter{AddedDeletedLength}{\tempstring}%%
    \fi%
    %enter here only when #1 is not \added or \deleted
    \ifnum\value{FLAGbignumber}=0%
            \relax%
            %enter here if #2 is an end line to split the bignumber in multiple lines
            \ifx#2\end%
                    %print #1 if only it is not part of \added{} or \deleted{}
                    \ifnum\value{AddedDeletedLength}<0%
                    {#1}%
                    \fi%
                    \ifnum\value{AddedDeletedLength}=0%
                    {#1}%
                    \fi%
            %enter here if #2 is NOT an end line, and add some glue to let the split in multiple lines
            \else%
                    %print #1 if only it is not part of \added{} or \deleted{}
                    \ifnum\value{AddedDeletedLength}<0%
                    {#1}\hspace{0pt plus 1pt}%
                    \fi%
                    \ifnum\value{AddedDeletedLength}=0%
                    {#1}\hspace{0pt plus 1pt}%
                    \fi%
            \fi%
    \fi%
    \ifnum\value{FLAGbignumber}=1%
                \let\next\@bignumber%
    \else%
            \ifx#2\end%
                \let\next\@gobble%
            %enter here if #2 is NOT an end line, and add some glue to let the split in multiple lines
            \else%
                \let\next\@bignumber%
            \fi%
    \fi%
    \ifnum\value{FLAGbignumber}=0%
        %Updating the length of AddedDeletedLength so to compute when #1 is no more part of \added{} or \deleted{}
        \def\AddedDeletedStringNEW{#1}%
        \StrLen{\AddedDeletedStringNEW}[\tempstring]%
        \setcounter{AddedDeletedLength}{\numexpr\value{AddedDeletedLength}-\tempstring\relax}%
    \fi%
    %FLAGbignumber is activated only once, at the beginning when \added{} or \deleted{} is encounter, in order to compute its length
    %othewise it is always zero
    \setcounter{FLAGbignumber}{0}%
    \next#2}%
\newcommand{\bignumber}[1]{\setcounter{AddedDeletedLength}{0}%
\def\temp{#1}\ifx\temp\empty\else\@bignumber#1\end\fi}
\makeatother

%This is a rework of the orginal bignumber function to make the string to appear with the \added{} font
\makeatletter
\def\@bignumberDeleted#1#2{%
  \ifx#2\end%
    \textcolor{red}{\st{#1}}\let\next\@gobble%
  \else%
    \textcolor{red}{\st{#1}}\hspace{0pt plus 1pt}\let\next\@bignumberDeleted%
  \fi%
  \next#2}
\newcommand{\bignumberDeleted}[1]{\def\temp{#1}\ifx\temp\empty\else\@bignumberDeleted#1\end\fi}
\makeatother

%This is a rework of the orginal bignumber function to make the string to appear with the \deleted{} font
\makeatletter
\def\@bignumberAdded#1#2{%
  \ifx#2\end%
    \textcolor{green}{\ul{#1}}\let\next\@gobble%
  \else%
    \textcolor{green}{\ul{#1}}\hspace{0pt plus 1pt}\let\next\@bignumberAdded%
  \fi%
  \next#2}
\newcommand{\bignumberAdded}[1]{\def\temp{#1}\ifx\temp\empty\else\@bignumberAdded#1\end\fi}
\makeatother


 
%%re-definition of href
\newcommand{\hrefMy}[2]{\href{#1}{\bignumber{#2}}}
  
%=========================================================
\begin{document}

  
\textbf{Example of using the hrefMy\{\} macro without any track change but with a very long URL spanning multiple lines:}

\hrefMy{https://www.google.com/}{very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url}

\textbf{Example of using the hrefMy\{\} macro with track changes by adding a very long URL:}

\hrefMy{https://www.google.com/}{\added{very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url}}

\textbf{Example of using the hrefMy\{\} macro with track changes by deleting a very long URL:}

\hrefMy{https://www.google.com/}{\deleted{very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url}}

\textbf{Example of using the hrefMy\{\} macro with track changes by adding and deleting a very long URL:}

\hrefMy{https://www.google.com/}{very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url\added{very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url}\deleted{very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url}}

\textbf{Another example of using the hrefMy\{\} macro with track changes:}

\hrefMy{https://www.google.com/}{very-long-url-\deleted{vveeery-long-url-}\added{vvery-long-url-}very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url-very-long-url}

\textbf{Another examples to test the use of consecutive characters:}

\hrefMy{https://www.google.com/}{abcdef}

\hrefMy{https://www.google.com/}{\added{abcdef}}

\hrefMy{https://www.google.com/}{\deleted{abcdef}}

\hrefMy{https://www.google.com/}{aabcdef}

\hrefMy{https://www.google.com/}{\added{aabcdef}}

\hrefMy{https://www.google.com/}{\deleted{aabcdef}}


\end{document}

相关内容