特定のフォントの行の高さに長さを設定する

特定のフォントの行の高さに長さを設定する

新しい長さを定義する方法:

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

特定のフォントの行の高さに等しい(例\tiny\ttfamily

注: 部分に何も書き込まないソリューションが必要です\begin{document}\end{document}

答え1

これはすべての文字 (0 から 255 まで) をループするため、 にのみ有効です。 を使用するpdflatexXeLaTeX または LuaLaTeX には、別のヒューリスティックが必要になりますfontspec

長さは、-variant が使用され\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}

ここに画像の説明を入力してください

関連情報