![LINK 文本中的连字符](https://linux22.com/image/388878/LINK%20%E6%96%87%E6%9C%AC%E4%B8%AD%E7%9A%84%E8%BF%9E%E5%AD%97%E7%AC%A6.png)
我同意SE 的岗位, 命令\link
。
\documentclass[10pt]{article}
\usepackage{bookmark}
\usepackage[table]{xcolor}
\usepackage{tabularx}
\newcommand{\link}[1]{%
{%
\def\textbf##1{##1}%
\def\textit##1{##1}%
\def\textcolor##1##2{##2}%
\def\protect##1##2{##2}%
\edef\_{\string_}%
\edef\&{\string&}%
\xdef\tmp{\noexpand\href{#1}}}%
\ttfamily%
\tmp{#1}%
}
\begin{document}
\begin{tabularx}{\linewidth}{|l|l|}
\link{https://www.\textcolor{blue}{longlink}.com/longlink/longlink/longlink/longlink/longlink/longlink/longlink/longlink/longlink/longlink/longlink/longlink/longlink/longlink/} & Long link description\tabularnewline
\link{www.\textcolor{blue}{longlinklonglinklonglinklonglinklonglinklonglinklonglinklonglinklonglinklonglinklonglinklonglinklonglinklonglinklonglinklonglink}.net} & Long link description\tabularnewline
\end{tabularx}
\end{document}
我得到:
那么,有两个问题:
- 我怎样才能更改文本以
\link
在斜线字符上获得连字符,而无需连字符?
如果没有斜线字符
- 我怎样才能更改文本以
\link
在任何地方获得连字符,但没有连字符?
答案1
想法是一样的,但是使用更强大的expl3
方法。
您可以获得与\link{<link>}
以前相同的结果,但有可能在斜线处断线。
您\link*{<link>}
可以允许在任何字母处断开。但是,这需要更改为\textcolor{blue}{<string>}
,\blue{<string>}
否则blue
也会被视为断开。为您需要的所有颜色定义类似的命令并更新等价项。
\documentclass[10pt]{article}
\usepackage{bookmark}
\usepackage[table]{xcolor}
\usepackage{tabularx}
\usepackage{xparse}
\NewDocumentCommand{\blue}{m}{\textcolor{blue}{#1}}
\ExplSyntaxOn
\NewDocumentCommand{\link}{sm}
{
\group_begin:
\cs_set_eq:NN \textbf \use:n
\cs_set_eq:NN \textit \use:n
\cs_set_eq:NN \blue \use:n
\cs_set:Npx \_ { \c_underscore_str }
\cs_set:Npx \& { \c_ampersand_str }
\tl_gset:Nx \g_tmpa_tl { #2 }
\group_end:
\tl_set:Nn \l_tmpa_tl { #2 }
\IfBooleanTF{#1}
{
\regex_replace_all:nnN { [A-Za-z] } { \0 \c{linebreak}[0] } \l_tmpa_tl
}
{
\tl_replace_all:Nnn \l_tmpa_tl { / } { /\linebreak[0] }
}
\group_begin:
\ttfamily
\exp_args:NVV \href \g_tmpa_tl \l_tmpa_tl
\group_end:
}
\ExplSyntaxOff
\begin{document}
\noindent
\begin{tabularx}{\linewidth}{|>{\raggedright}X|l|}
\link{https://www.\blue{longlink}.com/longlink/longlink/longlink/longlink/longlink/longlink/longlink/longlink/longlink/longlink/longlink/longlink/longlink/longlink/} & Long link description\tabularnewline
\link*{www.\blue{longlinklonglinklonglinklonglinklonglinklonglinklonglinklonglinklonglinklonglinklonglinklonglinklonglinklonglinklonglinklonglink}.net} & Long link description\tabularnewline
\end{tabularx}
\end{document}
当没有斜线时,也可以采用“处处中断”策略:
\documentclass[10pt]{article}
\usepackage{bookmark}
\usepackage[table]{xcolor}
\usepackage{tabularx}
\usepackage{xparse}
\NewDocumentCommand{\blue}{m}{\textcolor{blue}{#1}}
\ExplSyntaxOn
\NewDocumentCommand{\link}{m}
{
\group_begin:
\cs_set_eq:NN \textbf \use:n
\cs_set_eq:NN \textit \use:n
\cs_set_eq:NN \blue \use:n
\cs_set:Npx \_ { \c_underscore_str }
\cs_set:Npx \& { \c_ampersand_str }
\tl_gset:Nx \g_tmpa_tl { #1 }
\group_end:
\tl_set:Nn \l_tmpa_tl { #1 }
\regex_match:nnTF { / } { #1 }
{
\tl_replace_all:Nnn \l_tmpa_tl { / } { /\linebreak[0] }
}
{
\regex_replace_all:nnN { [A-Za-z] } { \0 \c{linebreak}[0] } \l_tmpa_tl
}
\group_begin:
\ttfamily
\exp_args:NVV \href \g_tmpa_tl \l_tmpa_tl
\group_end:
}
\ExplSyntaxOff
\begin{document}
\noindent
\begin{tabularx}{\linewidth}{|>{\raggedright}X|l|}
\link{https://www.\blue{example}.com/longlink/longlink/longlink/longlink/longlink/longlink/longlink/longlink/longlink/longlink/longlink/longlink/longlink/longlink/} & Long link description\tabularnewline
\link{www.\blue{longlinklonglinklonglinklonglinklonglinklonglinklonglinklonglinklonglinklonglinklonglinklonglinklonglinklonglinklonglinklonglink}.net} & Long link description\tabularnewline
\end{tabularx}
\end{document}
输出是一样的。