TransWikia.com

Change a font weight

TeX - LaTeX Asked by heblyx on July 29, 2021

I want to create a new font family with newfontfamily{rb}{Roboto} from here:
https://fonts.google.com/specimen/Roboto?subset=latin&preview.text=Hello&preview.text_type=custom#standard-styles

I want to write a few words with weight 300 for example, it is possible?
I saw this question: Set the "font-weight: lighter/ bolder"

But there are two things:

  1. I want to change it on the specific words, and not overall…
    so I’m looking for kind of a command like textbf or something like that.
  2. I want to use numbers (the number of the weight).

Thank you!

One Answer

weight command demo

Roboto weight 300 is called Roboto-Light.ttf, so, using the recommended standard weight series names (see texdoc fntguide), you can load it using fontspec as {l} series and then set up a textl{} command to use it.

Roboto weights

As to using numbers in command names, you can use Roman numerals (textiii):

weights

Or alternatively, set up a generic command that takes a numeric option, which in turn calls the matching text... command behind the scenes.

weight option

MWE

documentclass[12pt]{article}
newcommandmyfontname{Roboto}
usepackage[table]{xcolor}
usepackage{fontspec}
setmainfont[
    Path = c:/windows/fonts/, 
         Extension = .ttf,
    UprightFont     = *-Regular,
%    BoldFont = *-Bold,
    ItalicFont = *-Italic,
%    BoldItalicFont = *-BoldItalic,
    FontFace = {ul}{shapedefault}{*-Thin},
    FontFace = {ul}{it}{*-ThinItalic},
%    FontFace = {el}{shapedefault}{*-ExtraLight},
%    FontFace = {el}{it}{*-ExtraLightItalic},
    FontFace = {l}{shapedefault}{*-Light},
    FontFace = {l}{it}{*-LightItalic},
    FontFace = {sl}{shapedefault}{*-Regular},
    FontFace = {sl}{it}{*-Italic},
    FontFace = {m}{shapedefault}{*-Medium},
    FontFace = {m}{it}{*-MediumItalic},
%    FontFace = {sb}{shapedefault}{*-SemiBold},
%    FontFace = {sb}{it}{*-SemiBoldItalic},
    FontFace = {b}{shapedefault}{*-Bold},
    FontFace = {b}{it}{*-BoldItalic},
%    FontFace = {eb}{shapedefault}{*-ExtraBold},
%    FontFace = {eb}{it}{*-ExtraBoldItalic},
    FontFace = {ub}{shapedefault}{*-Black},
    FontFace = {ub}{it}{*-BlackItalic},
]{myfontname}

    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
DeclareRobustCommand{ulseries}{fontseries{ul}selectfont}    DeclareTextFontCommand{textul}{ulseries}
DeclareRobustCommand{lseries}{fontseries{l}selectfont}  DeclareTextFontCommand{textl}{lseries}
DeclareRobustCommand{slseries}{fontseries{sl}selectfont}    DeclareTextFontCommand{textsl}{slseries}
DeclareRobustCommand{mseries}{fontseries{m}selectfont}  DeclareTextFontCommand{textm}{mseries}
DeclareRobustCommand{bseries}{fontseries{b}selectfont}  DeclareTextFontCommand{textb}{bseries}
    
DeclareRobustCommand{ubseries}{fontseries{ub}selectfont}    DeclareTextFontCommand{textub}{ubseries}

newcommand{texti}[1]{textul{#1}}
newcommand{textiii}[1]{textl{#1}}
newcommand{textiv}[1]{textsl{#1}}
newcommand{textv}[1]{textm{#1}}
newcommand{textvii}[1]{textb{#1}}
newcommand{textix}[1]{textub{#1}}

expandafternewcommandcsname text100endcsname[1]{textul{#1}}
expandafternewcommandcsname text300endcsname[1]{textl{#1}}
expandafternewcommandcsname text400endcsname[1]{textsl{#1}}
expandafternewcommandcsname text500endcsname[1]{textm{#1}}
expandafternewcommandcsname text700endcsname[1]{textb{#1}}
expandafternewcommandcsname text900endcsname[1]{textub{#1}}

newcommandtextw[2][400]{csname text#1endcsname{#2}}




newcommandtesttext{{large abc qwerty}}%  fontnamefont}

begin{document}

{Largebfseriesmyfontname}

%end{tabular}
%------------------------------
begin{tabular}{llll}
hline
rowcolor{blue!12}
Font & Series & Upright & Italic 
100 ultralight & 
ul  & 
fontseries{ul}selectfonttesttext  &
fontseries{ul}fontshape{it}selectfonttesttext  
%%
%extralight & 
%el & 
%fontseries{el}selectfonttesttext  &
%fontseries{el}fontshape{it}selectfonttesttext  
%%
300 light & 
l & 
fontseries{l}selectfonttesttext  &
fontseries{l}fontshape{it}selectfonttesttext  
%
400 semilight & 
sl & 
fontseries{sl}selectfonttesttext  &
fontseries{sl}fontshape{it}selectfonttesttext  
%
500 medium & 
m & 
fontseries{m}selectfonttesttext  &
fontseries{m}fontshape{it}selectfonttesttext  
%%
%semibold & 
%sb & 
%fontseries{sb}selectfonttesttext  &
%fontseries{sb}fontshape{it}selectfonttesttext  
%%
700 bold & 
b & 
fontseries{b}selectfonttesttext  &
fontseries{b}fontshape{it}selectfonttesttext  
%
%extrabold & 
%eb & 
%fontseries{eb}selectfonttesttext  &
%fontseries{eb}fontshape{it}selectfonttesttext  
%%
900 ultrabold & 
ub & 
fontseries{ub}selectfonttesttext  &
fontseries{ub}fontshape{it}selectfonttesttext  
hline
end{tabular}

bigskip
scshape
%------------------------------
begin{tabular}{llll}
hline
rowcolor{blue!12}
Font & Series & Upright & Italic 
ultralight & 
ul  & 
fontseries{ul}selectfonttesttext  &
fontseries{ul}fontshape{it}selectfonttesttext  
%
%extralight & 
%el & 
%fontseries{el}selectfonttesttext  &
%fontseries{el}fontshape{it}selectfonttesttext  
%%
light & 
l & 
fontseries{l}selectfonttesttext  &
fontseries{l}fontshape{it}selectfonttesttext  
%
semilight & 
sl & 
fontseries{sl}selectfonttesttext  &
fontseries{sl}fontshape{it}selectfonttesttext  
%
medium & 
m & 
fontseries{m}selectfonttesttext  &
fontseries{m}fontshape{it}selectfonttesttext  
%
%semibold & 
%sb & 
%fontseries{sb}selectfonttesttext  &
%fontseries{sb}fontshape{it}selectfonttesttext  
%%
bold & 
b & 
fontseries{b}selectfonttesttext  &
fontseries{b}fontshape{it}selectfonttesttext  
%
%extrabold & 
%eb & 
%fontseries{eb}selectfonttesttext  &
%fontseries{eb}fontshape{it}selectfonttesttext  
%%
ultrabold & 
ub & 
fontseries{ub}selectfonttesttext  &
fontseries{ub}fontshape{it}selectfonttesttext  
hline
end{tabular}

bigskip
textl{This is 300 weight.}
upshape
textl{This is 300 weight.} textbackslash textl{}


bigskip
scshape
textiii{This is 300 weight.}
upshape

texti{This is 100 weight.} textbackslash texti{}

textiii{This is 300 weight.} textbackslash textiii{}

textiv{This is 400 weight.} textbackslash textiv{}

textv{This is 500 weight.} textbackslash textv{}

textvii{This is 700 weight.} textbackslash textvii{}

textix{This is 900 weight.} textbackslash textix{}

bigskip
xyz texti{abcde} textiii{abcde} textiv{abcde} textv{abcde} 
textvii{abcde} textix{abcde} xyz

bigskip
textw[900]{fghijk} = verb|textw[900]{fghijk}|

textw[300]{fghijk} = verb|textw[300]{fghijk}|


end{document}




Answered by Cicada on July 29, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP