代码之家  ›  专栏  ›  技术社区  ›  Konrad Rudolph

根据使用的字体有条件地执行宏

  •  5
  • Konrad Rudolph  · 技术社区  · 16 年前

    我是一个很大的球迷,所以我把它的建议 “use the best available ampersand”

    为此,我在LaTeX中定义了以下快捷方式:

    \let\amp\&
    \renewcommand\&{{\scalebox{1.2}{\textnormal{\fontspec{Baskerville}\itshape\amp}}}}
    

    This is a text \& it contains an ampersand.
    

    (使用 \& & 因为LaTeX就是这样工作的,所以后者已经被保留为表环境中的独立列。)

    防止

    {\sffamily a \& b}
    {\ttfamily a \& b}
    

    我想象如下:

    \renewcommand\&{
      \ifsans
        {\fontspec{Trebuchet MS}{\textnormal{\itshape\amp}}}
      \else
        \ifmono
          \amp
        \else
          {\fontspec{Baskerville}\scalebox{1.2}{\textnormal{\itshape\amp}}}
        \fi
      \fi}
    
    2 回复  |  直到 16 年前
        1
  •  4
  •   phimuemue    16 年前

    其工作方式如下:

    \documentclass[letterpaper,10pt]{article}
    \usepackage[latin1]{inputenc}
    \usepackage{german}
    \usepackage{geometry}
    \geometry{margin=2cm}
    \newcommand*\origsffamily{}
    \let\origsffamily\sffamily
    \renewcommand*\sffamily{\origsffamily\small {\renewcommand\&{{\scalebox{1.2}{\textnormal{\fontspec{Baskerville}\itshape\amp}}}}}}
    
    \begin{document}
    
    
    hello, this is \& a \sffamily test \& 
    
    
    \end{document}
    

    \sffamily .

    缺点是-当然-你必须为所有想要的字体系列这样做。

        2
  •  2
  •   Philipp    16 年前

    您可以测试标准的乳胶令牌列表 \f@family 但是,这可能不像您希望的那样可靠。下面的代码只是检查当前族是否等于 \setmainfont \setsansfont ,但不是字体是否真的无衬线:

    \documentclass{article}
    
    \usepackage{fontspec}
    \usepackage{expl3}
    \usepackage{xparse}
    
    \setmainfont{DejaVu Serif}
    \setsansfont{DejaVu Sans}
    
    \makeatletter
    \ExplSyntaxOn
    \NewDocumentCommand \amp { } {
      \tl_if_eq:NNTF \f@family \rmdefault {
        % this is a roman font
        A
      } {
        \tl_if_eq:NNTF \f@family \sfdefault {
          % this is a sans font
          B
        } {
          % something else
          C
        }
      }
    }
    \ExplSyntaxOff
    \makeatother
    
    
    \begin{document}
    
    test \amp\ test
    
    \sffamily
    test \amp\ test
    
    \ttfamily
    test \amp\ test
    
    \end{document}