我使用以下命令设置自定义 gls 文本格式:
\renewcommand*{\glstextformat}[1]{\textcolor{red}{\textbf{#1}}}
但是,有时,我不希望在本地使用这种格式,例如 Tikzpicture、表格或词汇表。
这里是 MWE:
\documentclass{article}
\usepackage[style=tree]{glossaries-extra}
\usepackage{xcolor}
\usepackage{tikz}
\makenoidxglossaries
\newglossaryentry{subsytem}{name={Subssytems},description={\glspar},sort={4}}
\newglossaryentry{compressor}
{
name={Compressor},
text={compressor},
sort={compressor},
description={Air Compressor},
symbol={cp},
parent=subsytem
}
\newglossaryentry{compressor_motor}
{
name={Compressor Motor},
sort={compressor motor},
text={compressor motor},
description={Motor of the \Gls{compressor}},
symbol={cm},
parent=subsytem
}
% My custom gls text format !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
\renewcommand*{\glstextformat}[1]{\textcolor{red}{\textbf{#1}}}
\begin{document}
In text I want custom gls format (bold and red) :
\gls{compressor_motor}
\gls{compressor}
But in tables, tikzfigure, glossary I don't want it (I want default format) :\\
\begin{tabular}{rr}
\hline
\Glssymbol{compressor} & 1 \\
\hline
1 & 2.36 \\
\hline
\end{tabular}
\begin{tikzpicture}%
\node (CpMap) [draw, fill=blue!20, text width=5em,text centered, minimum height=2.5em] {\Glssymbol{compressor}};
\end{tikzpicture}%
\printnoidxglossary
\end{document}
因此,结果并非如此:
我想要这样的东西:
我通过用这两行(例如使用 Tikzfigure)括住我不想使用 gls 格式的部分来获得此结果:
\renewcommand*{\glstextformat}[1]{{#1}}% Set none gls text format
\begin{tikzpicture}%
\node (CpMap) [draw, fill=blue!20, text width=5em,text centered, minimum height=2.5em] {\Glssymbol{compressor}};
\end{tikzpicture}%
\renewcommand*{\glstextformat}[1]{\textcolor{red}{\textbf{#1}}}% Restore custom gls text format
这确实不切实际。
我尝试创建一个新的环境来实现这种封闭,但是没有成功......
编辑 #1 01/28/22 - 澄清:我指定我最常使用“彩色”格式(参见开头)。因此,当我使用所有经典命令(、、等)时,我希望此格式“默认”可用。\Gls
(这就是我用重新定义 gls 链接的全局默认格式的原因。)并且仅在某些情况下,我希望能够删除此“彩色”格式,转而使用我选择的另一种格式(“normalfont”也足以满足我的需求)。事实上,当我最常使用“彩色”格式时,使用其他命令(经典 gls 命令除外)对我来说太麻烦了。相反,当我不需要“彩色”格式时,我不介意必须进行额外的操作,因为这对我来说是一种罕见的情况……\gls
\glssymbol
\glstextformat
编辑 #2 01/28/22 - 反馈:DG 的解决方案(使用\gls[textformat={normalfont}]{compressor}
)回答了我的问题。但是,经过一些测试,我发现它不太适合我的需求(在我的原始问题中没有完全看到)。事实上,我经常用另一个来定义一个 gls 条目,例如(例如使用字段name
和字段description
):
\newglossaryentry{compressor_motor}
{
name={\glsname[textformat={normalfont}]{compressor} Motor},
sort={compressor motor},
text={compressor motor},
description={Motor of the \Gls[textformat={normalfont}]{compressor}},
symbol={cm},
parent=subsytem
}
因此,如果我这样做,我将获得以下结果:
使用以下代码:
\documentclass{article}
\usepackage[style=tree]{glossaries-extra}
\usepackage{xcolor}
\makenoidxglossaries
\newglossaryentry{subsytem}{name={Subssytems},description={\glspar},sort={4}}
\newglossaryentry{compressor}
{
name={Compressor},
text={compressor},
sort={compressor},
description={Air Compressor},
symbol={cp},
parent=subsytem
}
\newglossaryentry{compressor_motor}
{
name={\glsname[textformat={normalfont}]{compressor} Motor},
sort={compressor motor},
text={compressor motor},
description={Motor of the \Gls[textformat={normalfont}]{compressor}},
symbol={cm},
parent=subsytem
}
% My custom gls text format !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
\renewcommand*{\glstextformat}[1]{\textcolor{red}{\textbf{#1}}}
\begin{document}
\glsdesc{compressor_motor}
\printnoidxglossary
\end{document}
正如您所看到的,出现了一些字体(粗体)问题(只有红色省略号,矩形可以),正如 Simon Dispa 在评论中指出的那样。但该[textformat={normalfont}]
解决方案非常有效,因为它允许在文本中使用红色(当\glsdesc
使用时),但不能使用粗体格式。
所以,我不知道该怎么办……也许我可以使用 Simon Dispa 解决方案并创建新字段(如\glscol
、\glssymbolcol
\glsnamecol
等),以满足我的所有需求,并且不要通过重新定义命令来触及全局字体\glstextformat
。但是,这种方法对于日常使用来说相当麻烦……
我很高兴能有一个解决方案,可以在本地将全局 gls 链接字体更改为新环境或其他东西...但如果不可能的话,我准备放手了...
答案1
您可以定义一个新键,而不必重新定义全局默认格式。
新的键(col
)允许定义\glscol
应用粗体和颜色。
\documentclass{article}
\usepackage[style=tree]{glossaries-extra}
%*************************************** added <<<<<<<<<<<<<<<<<<
\glsaddkey*% default value needs expanding
{col}% key
{\glsentrytext{\glslabel}col}% default value
{\glsentrycol}% command analogous to \glsentrytext
{\Glsentrycol}% command analogous to \Glsentrytext
{\glscol}% command analogous to \glstext
{\Glscol}% command analogous to \Glstext
{\GLScol}% command analogous to \GLStext
%***************************************
\usepackage{xcolor}
\usepackage{tikz}
\makenoidxglossaries
\newglossaryentry{subsytem}{name={Subssytems},description={\glspar},sort={4}}
\newglossaryentry{compressor}
{
name={Compressor},
sort={compressor},
text={compressor},
col={\bfseries \textcolor{red}{compressor}}, % added <<<<<<<<<<<<<<<<<
description={Air Compressor},
symbol={cp},
parent=subsytem
}
\newglossaryentry{compressor_motor}
{
name={Compressor Motor},
sort={compressor motor},
text={compressor motor},
col={\bfseries \textcolor{red}{compressor motor}},
description={Motor of the \Gls{compressor}}, , % added <<<<<<<<<<<<<<<<<
symbol={cm},
parent=subsytem
}
\begin{document}
In text I want custom gls format (bold and red) :
\glscol{compressor_motor} % changed <<<<<<<<<<<<<<<<<<<<<<<<<
\glscol{compressor} % changed <<<<<<<<<<<<<<<<<<<<<<<<<
But in tables, tikzfigure, glossary I don't want it (I want default format) :\\
\begin{tabular}{rr}
\hline
\Glssymbol{compressor} & 1 \\
\hline
1 & 2.36 \\
\hline
\end{tabular}
\begin{tikzpicture}%
\node (CpMap) [draw, fill=blue!20, text width=5em,text centered, minimum height=2.5em] {\Glssymbol{compressor}};
\end{tikzpicture}%
\printnoidxglossary
\end{document}
答案2
您glossaries-extra
可以将可选参数传递textformat
给gls
类似命令来在本地格式化条目:
\documentclass{article}
\usepackage[style=tree]{glossaries-extra}
\usepackage{xcolor}
\usepackage{tikz}
\makenoidxglossaries
\newglossaryentry{subsytem}{name={Subssytems},description={\glspar},sort={4}}
\newglossaryentry{compressor}
{
name={Compressor},
text={compressor},
sort={compressor},
description={Air Compressor},
symbol={cp},
parent=subsytem
}
\newglossaryentry{compressor_motor}
{
name={Compressor Motor},
sort={compressor motor},
text={compressor motor},
description={Motor of the \Gls{compressor}},
symbol={cm},
parent=subsytem
}
% My custom gls text format !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
\renewcommand*{\glstextformat}[1]{\textcolor{red}{\textbf{#1}}}
\begin{document}
In text I want custom gls format (bold and red) :
\gls{compressor_motor}
\gls{compressor}
But in tables, tikzfigure, glossary I don't want it (I want default format) :\\
\begin{tabular}{rr}
\hline
\Glssymbol{compressor} & 1 \\
\hline
1 & 2.36 \\
\hline
\end{tabular}
\begin{tikzpicture}%
\node (CpMap) [draw, fill=blue!20, text width=5em,text centered, minimum height=2.5em] {\Glssymbol[textformat={normalfont}]{compressor}};
\end{tikzpicture}%
\printnoidxglossary
\end{document}
您还可以使用自己的格式化命令,如blackbold
本例所示:
\documentclass{article}
\usepackage[style=tree]{glossaries-extra}
\usepackage{xcolor}
\usepackage{tikz}
\makenoidxglossaries
% My custom gls text format !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
\renewcommand*{\glstextformat}[1]{\textcolor{red}{\textbf{#1}}}
\newcommand{\blackbold}[1]{\textcolor{black}{\textbf{#1}}}
\newglossaryentry{subsytem}{name={Subssytems},description={\glspar},sort={4}}
\newglossaryentry{compressor}
{
name={Compressor},
text={compressor},
sort={compressor},
description={Air Compressor},
symbol={cp},
parent=subsytem
}
\newglossaryentry{compressor_motor}
{
name={\glsname[textformat={blackbold}]{compressor} Motor},
sort={compressor motor},
text={compressor motor},
description={Motor of the \Gls[textformat={blackbold}]{compressor}},
symbol={cm},
parent=subsytem
}
\begin{document}
In text I want custom gls format (bold and red) :
\gls{compressor_motor}
\gls{compressor}
But in tables, tikzfigure, glossary I don't want it (I want default format) :\\
\begin{tabular}{rr}
\hline
\Glssymbol{compressor} & 1 \\
\hline
1 & 2.36 \\
\hline
\end{tabular}
\begin{tikzpicture}%
\node (CpMap) [draw, fill=blue!20, text width=5em,text centered, minimum height=2.5em] {\Glssymbol[textformat={normalfont}]{compressor}};
\end{tikzpicture}%
\printnoidxglossary
\end{document}
答案3
更新 1:带有可选参数的环境
我找到了如何根据自己的需要创建一个新的环境(如果它可以帮助某人)!
首先,我为全局和本地自定义词汇表链接格式创建了 3 个命令:
- 全球的gls 格式(血红色):
\newcommand*{\myGLOBALDefaultGlsTextFormat}[1]{{\bfseries{\textcolor{red}{#1}}}}
- 本地默认gls 格式(斜体绿色):
\newcommand*{\myLocalDEFAULTGlsTextFormat}[1]{\textit{\textcolor{green}{#1}}}
- 当地习俗gls 格式(蓝色下划线):
\newcommand*{\myLocalCUSTOMGlsTextFormat}[1]{\underline{{\textcolor{blue}{#1}}}}
我将全局 gls 链接格式设置为粗体红色:
\renewcommand*{\glstextformat}[1]{\myGLOBALDefaultGlsTextFormat{#1}}% Define global gls links format
然后是带有可选参数的新环境(需要xparse
包):
放本地默认gls 格式(斜体绿色)如果不争论已结束;
放当地习俗gls 格式 (蓝色下划线)如果例如,命令
\myLocalCUSTOMGlsTextFormat
作为参数传递(顺便说一下,任何其他格式都可以传递);
这里是环境声明代码(参见给出答案详情请见):
\usepackage{xparse}
\NewDocumentEnvironment{localGlsTextFormat}{o}%
{\IfNoValueTF{#1}{%start code no opt arg
\let\oldglstextformat\glstextformat
\renewcommand\glstextformat[1]{\myLocalDEFAULTGlsTextFormat{##1}}}{%start code with #1
\let\oldglstextformat\glstextformat
\renewcommand\glstextformat[1]{#1{##1}}}}%
{\IfNoValueTF{#1}{%end code no opt arg
}{%end code with #1
}}%
这里是另一个没有可选参数的环境(不需要xparse
包)只是设置本地默认如果没有给出可选参数,则使用 gls 格式(斜体绿色),如上述环境所示:
\newenvironment*{defaultLocalGlsTextFormat}{%
\let\oldglstextformat\glstextformat
\renewcommand\glstextformat[1]{\myLocalDEFAULTGlsTextFormat{##1}}%Set Local format
}%
这里是 MWE:
\documentclass{article}
\usepackage[style=tree]{glossaries-extra}
\usepackage{xcolor}
\usepackage{tikz}
\makenoidxglossaries
\newcommand*{\myGLOBALDefaultGlsTextFormat}[1]{{\bfseries{\textcolor{red}{#1}}}}% Global format bold red
\newcommand*{\myLocalDEFAULTGlsTextFormat}[1]{\textit{\textcolor{green}{#1}}}% Local format italic green
\newcommand*{\myLocalCUSTOMGlsTextFormat}[1]{\underline{{\textcolor{blue}{#1}}}}% Local format underline blue
\renewcommand*{\glstextformat}[1]{\myGLOBALDefaultGlsTextFormat{#1}}% Define global gls links format
\usepackage{xparse}
% New environment for local gls link format WITH optionnal argument
\NewDocumentEnvironment{localGlsTextFormat}{o}%
{\IfNoValueTF{#1}{%start code no opt arg
\let\oldglstextformat\glstextformat
\renewcommand\glstextformat[1]{\myLocalDEFAULTGlsTextFormat{##1}}}{%start code with #1
\let\oldglstextformat\glstextformat
\renewcommand\glstextformat[1]{#1{##1}}}}%
{\IfNoValueTF{#1}{%end code no opt arg
}{%end code with #1
}}%
% New environment for local gls link format WITHOUT optionnal argument
\newenvironment*{defaultLocalGlsTextFormat}{%
\let\oldglstextformat\glstextformat
\renewcommand\glstextformat[1]{\myLocalDEFAULTGlsTextFormat{##1}}%Set Local format
}
% Glossary entries
\newglossaryentry{subsytem}{name={Subssytems},description={\glspar},sort={4}}
\newglossaryentry{compressor}
{
name={Compressor},
text={compressor},
sort={compressor},
description={Air Compressor},
symbol={cp},
parent=subsytem
}
\newglossaryentry{compressor_motor}
{
name={\glsname{compressor} Motor},
sort={compressor motor},
text={compressor motor},
description={Motor of the \Gls{compressor}},
symbol={cm},
parent=subsytem
}
\begin{document}
\textbf{Here global gls formated links (bold red)}:
\glsdesc{compressor_motor};
\glstext{compressor_motor}
\begin{tabular}{rr}
\hline
\Glssymbol{compressor} & 1 \\
\hline
1 & 2.36 \\
\hline
\end{tabular}
\begin{tikzpicture}%
\node (CpMap) [draw, fill=blue!20, text width=5em,text centered, minimum height=2.5em] {\Glssymbol{compressor}};
\end{tikzpicture}%
\printnoidxglossary
\vspace{1cm}
\textbf{Here local default gls formated links (italic green) given by the environment without optional argument (no need for xparse package)}:
\begin{defaultLocalGlsTextFormat}
\begin{tabular}{rr}
\hline
\Glssymbol{compressor} & 1 \\
\hline
1 & 2.36 \\
\hline
\end{tabular}
\end{defaultLocalGlsTextFormat}
\vspace{1cm}
\textbf{Here local default gls formated links (italic green) with the xparse environment but without passing an optionnal argument}:
\begin{localGlsTextFormat}
\glsdesc{compressor_motor};
\glstext{compressor_motor}
\end{localGlsTextFormat}
\vspace{1cm}
\textbf{Here local custom gls formated links (underline blue) with the xparse environment with an optionnal argument}:
\begin{localGlsTextFormat}[\myLocalCUSTOMGlsTextFormat]
\begin{tikzpicture}%
\node (CpMap) [draw, fill=blue!20, text width=5em,text centered, minimum height=2.5em] {\Glssymbol{compressor}};
\end{tikzpicture}%
\printnoidxglossary
\end{localGlsTextFormat}
\vspace{1cm}
\textbf{Here again global gls formated links}:
\glsdesc{compressor_motor};
\glstext{compressor_motor}
\end{document}
结果: