将长度设置为特定字体的行高

将长度设置为特定字体的行高

如何使用定义新长度:

\newlength{...}
\setlength{...}{...}

并且等于特定字体的行高(例如\tiny\ttfamily

注意:我想要一个不涉及在该\begin{document}\end{document}部分写任何内容的解决方案。

答案1

这将循环遍历所有字符(从 0 到 255),因此它只适用于pdflatex;XeLaTeX 或 LuaLaTeX 需要一些不同的启发式方法fontspec

长度\fonttotalheight定义为高度加上深度,除非*使用-variant,否则将仅测量高度,而不考虑下降部分。

\documentclass[12pt]{article}
\usepackage[T1]{fontenc}

\newlength{\fonttotalheight}

\makeatletter
\newcommand{\setfonttotalheight}{%
  \@ifstar{\@tempswafalse\setfont@totalheight}%
          {\@tempswatrue\setfont@totalheight}%
}
\newcommand{\setfont@totalheight}[1][]{%
  \sbox{\z@}{%
    \normalfont
    #1% it should be a font selection command
    \selectfont 
    \count@=\z@
    \loop
      \char\count@
    \ifnum\count@<\@cclv
      \advance\count@\@ne
    \repeat
  }%
  \setlength\fonttotalheight{\ht\z@}%
  \if@tempswa\addtolength\fonttotalheight{\dp\z@}\fi
 }
\makeatother

\setfonttotalheight*

\begin{document}

Initial value: \texttt{\the\fonttotalheight} (only height)

\bigskip

\begingroup
Fontencoding: OT1
\fontencoding{OT1}\selectfont

\setfonttotalheight*
Normal font (only height): \texttt{\the\fonttotalheight}

\setfonttotalheight
Normal font (height and depth): \texttt{\the\fonttotalheight}

\setfonttotalheight[\ttfamily]
Typewriter type (height and depth): \texttt{\the\fonttotalheight}

\endgroup

\bigskip

Fontencoding: T1

\setfonttotalheight*
Normal font (only height): \texttt{\the\fonttotalheight}

\setfonttotalheight
Normal font (height and depth): \texttt{\the\fonttotalheight}

\setfonttotalheight[\ttfamily]
Typewriter type (height and depth): \texttt{\the\fonttotalheight}

\end{document}

在此处输入图片描述

答案2

我尝试了该\totalheightof命令,并将长度的定义及其设置包装到命令中。希望这会有所帮助!

\documentclass[12pt]{scrbook}
\usepackage{etoolbox}
\usepackage{ifmtarg}
\usepackage{calc}

\def\FontHeightDummyText{ABBAASAADSDAgss,qppptssdfaf23l4j324jl234jklsjdflsdkfjslakfjslfsdajfdsljfslkdfjl23k42j34}


\makeatletter
\providecommand{\ProvideLength}[2][]{%
% Check, if the command is already defined, if not, then define it!
\ifdeflength{#2}{% It is already defined!
\GenericWarning{}{Warning: Length #2 already defined!!!!!!!!} % Optional
}{% Not defined, so define it!
\newlength{#2}%
}%
\@ifmtarg{#1}{%  is 1st argument empty -> do not set the length at all!
}{% Set the length to the value of the 1st argument.
\setlength{#2}{#1}%  
}% End of \@ifmtarg
}% End of \providecommand
\makeatother


% Setting the length as 1st parameter and initialize it
% with the value of the second arg. (hopefully a length ;-))
\newrobustcmd{\SetHeightFromTextTemplate}[2]{%
\ProvideLength[\totalheightof{#2}]{#1}%
}%

%\SetHeightFromTextTemplate{} can be done here also

\begin{document}
\begin{flushleft}

\SetHeightFromTextTemplate{\FontHeight}{\FontHeightDummyText}
Height of font is \the\FontHeight 

Using \(\backslash\)Large \Large
%%% Recalculate
\SetHeightFromTextTemplate{\FontHeight}{\FontHeightDummyText}
%\setlength{\FontHeight}{\totalheightof{\FontHeightDummyText}}

Height of font is \the\FontHeight 



Now with \begin{verbatim}\tiny\ttfamily:\end{verbatim} 

\begingroup
\tiny\ttfamily
%%% Recalculate
\SetHeightFromTextTemplate{\FontHeight}{\FontHeightDummyText}


Height of font is \the\FontHeight
\endgroup

\end{flushleft}

\end{document}

在此处输入图片描述

相关内容