多语言文档中的 pgfmathsetlengthmacro 带有宽度(“...”)

多语言文档中的 pgfmathsetlengthmacro 带有宽度(“...”)

下面是一个代码,其中tikz width("")使用函数/宏来获取字符串的长度并将其存储到长度变量中pgfmathsetlengthmacro

似乎在英语环境中设置的长度变量只能在英语环境中使用,用于设置矩阵宽度或仅用于打印。第二种语言也一样。即使将\ll下面代码中的内容分配给 tex 长度变量以在 \textenglish{...} 宏中打印它也会失败。

有没有办法能够在设置该长度的语言环境之外的其他语言环境中使用该长度width("")?我的文档的主要语言是外语,矩阵包含两种语言的文本,并且大多数情况下英文文本最长,因此必须使用它来设置列宽。

matrix对于的灵活性,tikz我不想使用NiceTabular。在这种情况下,无需手动确定列的宽度。

\documentclass{beamer}

\mode<presentation>
{    \usetheme{metropolis}    }

\newlength{\slw}\setlength{\slw}{160mm}
\newlength{\slh}\setlength{\slh}{90mm}
\geometry{verbose,papersize={\slw,\slh}}

\usepackage{fontsize,fix-cm}

\usepackage{polyglossia}
\newfontfamily\devanagarifont[Script=Devanagari]{NotoSerifDevanagari}
\newfontfamily\devanagarifontsf[Script=Devanagari]{NotoSansDevanagari}

\setotherlanguages{english}
\newfontfamily\englishfontsf{NotoSans}
\newcommand{\eng}[1]{\textenglish{#1}}

\setmainlanguage{sanskrit}
\setmainfont[Script=Devanagari]{NotoSansDevanagari}
\usepackage{tikz}
\usetikzlibrary{calc,matrix,math}

  \tikzset{
  allmatrix/.style = {matrix of nodes,
                      nodes in empty cells,matrix anchor=#1,
                      every outer matrix/.append style={
                        inner sep=0pt, outer sep=0pt},
                      row sep=-\pgflinewidth, column sep=-\pgflinewidth,
                      ampersand replacement=\&, nodes={outer sep=0pt},
                      text height=1.5ex, text depth=.25ex},
  }

\newlength{\texlength}
\begin{document}
\begin{frame}
  \begin{tikzpicture}

    \begin{english} 
      \pgfmathsetlengthmacro{\ll}{ width("iiiiii") } 
      \setlength\texlength{\ll}
      \matrix at (0,0) [allmatrix={north}, draw,
        nodes={text width=7*\texlength}] (eng)
        {
          \ll \\           % here \ll is printed normally
          \the\texlength \\  % 
        };
    \end{english}
    
    \pgfmathsetlengthmacro{\ls}{ width("लघु") } 
    \matrix at (5,0) [allmatrix={north}, draw,
      nodes={text width=10*\ls}] (sam) %here \ll instead of \ls will not work
      {  संस्कृतं मधुरम् अस्ति। \ll \\         % here \ll is not printed
        \eng{text in english \ll} \\ 
        \eng{ \the\texlength } \\ };% This fails as it shows "0.0 pt"
  \end{tikzpicture}
\end{frame}
\end{document}

答案1

使用 LaTeX,长度分配是本地的,仅限于当前组并\begin{english}...\end{english}形成一个组。

最简单的方法是\texlength在设置之后使其成为全局的:

\setlength{\texlength}{\ll}
\global\texlength=\texlength

您还可以\textlength在组内设置其所有用途,例如在之后\begin{frame}

\settowidth{\texlength}{\textlang{english}{iiiiii}}

完整代码,包含第二种解决方案:

\documentclass{beamer}

\mode<presentation>
{    \usetheme{metropolis}    }

\newlength{\slw}\setlength{\slw}{160mm}
\newlength{\slh}\setlength{\slh}{90mm}
\geometry{verbose,papersize={\slw,\slh}}

\usepackage{fontsize,fix-cm}

\usepackage{polyglossia}
\newfontfamily\devanagarifont[Script=Devanagari]{NotoSerifDevanagari}
\newfontfamily\devanagarifontsf[Script=Devanagari]{NotoSansDevanagari}

\setotherlanguages{english}
\newfontfamily\englishfontsf{NotoSans}
\newcommand{\eng}[1]{\textenglish{#1}}

\setmainlanguage{sanskrit}
\setmainfont[Script=Devanagari]{NotoSansDevanagari}
\usepackage{tikz}
\usetikzlibrary{calc,matrix,math}

  \tikzset{
  allmatrix/.style = {matrix of nodes,
                      nodes in empty cells,matrix anchor=#1,
                      every outer matrix/.append style={
                        inner sep=0pt, outer sep=0pt},
                      row sep=-\pgflinewidth, column sep=-\pgflinewidth,
                      ampersand replacement=\&, nodes={outer sep=0pt},
                      text height=1.5ex, text depth=.25ex},
  }

\newlength{\texlength}
\begin{document}
\begin{frame}

  \settowidth{\texlength}{\textlang{english}{iiiiii}}

  \begin{tikzpicture}

    \begin{english}
      \pgfmathsetlengthmacro{\ll}{ width("iiiiii") }
      \matrix at (0,0) [allmatrix={north}, draw,
        nodes={text width=7*\texlength}] (eng)
        {
          Inside \texttt{english} env.\\
          \ll                 % here \ll is printed normally
          {\footnotesize~\texttt{\textbackslash ll}}\\
          \the\texlength      % \texlength and \ll are equal
          {\footnotesize~\texttt{\textbackslash texlength}}\\
        };
    \end{english}

    \pgfmathsetlengthmacro{\ls}{ width("लघु") }
    \matrix at (5,0) [allmatrix={north}, draw,
      nodes={text width=10*\ls}] (sam)
      { \eng{Outside \texttt{english} env.}\\
        संस्कृतं मधुरम् अस्ति। \\
        \eng{text in english} \\
        \eng{ \ll             % \ll is undefined here (shows nothing)
          {\footnotesize~\texttt{\textbackslash ll} (undefined here)}
        } \\
        \eng{ \the\texlength  % \texlength is defined here (shows "15.48001pt")
          {\footnotesize~\texttt{\textbackslash texlength}}
        } \\ };

    \end{tikzpicture}
\end{frame}
\end{document}

在公共组中设置宽度

相关内容