如何呈现unicode符号?

如何呈现unicode符号?

我有以下乳胶代码。

\documentclass{article}
\usepackage[utf8]{inputenc}

\begin{document}

\begin{verbatim}
█ [1:0x561b3f574358] <list>
├─[2:0x561b3e9cd938] <dbl>
├─[3:0x561b3e9cd900] <dbl>
└─[4:0x561b3e9cd8c8] <dbl>
\end{verbatim}
\end{document}

渲染结果如下,左侧符号未显示。

在此处输入图片描述

答案1

我建议使用 XeLaTeX 或 LuaLaTeX 以及提供所用符号的字体,例如

% Use this with XeLaTeX or LuaLaTeX not PDFLaTeX.
\documentclass{article}
\usepackage{libertinus}

\begin{document}

\begin{verbatim}
█ [1:0x561b3f574358] <list>
├─[2:0x561b3e9cd938] <dbl>
├─[3:0x561b3e9cd900] <dbl>
└─[4:0x561b3e9cd8c8] <dbl>
\end{verbatim}
\end{document}

使用 LuaLaTeX 或 XeLaTeX

如果您想使用 PDFLaTeX,您可以声明缺少的 unicode 字符,例如使用\rule命令:

% Use this with PDFLaTeX, not XeLaTeX or LuaLaTeX!
\documentclass{article}
\DeclareUnicodeCharacter{2588}{\rule{1ex}{\ht\strutbox}}
\DeclareUnicodeCharacter{251C}{\rule[-\dp\strutbox]{.4pt}{\baselineskip}%
  \rule[.2\baselineskip]{1ex}{.4pt}}
\DeclareUnicodeCharacter{2500}{\rule[.2\baselineskip]{1ex}{.4pt}}
\DeclareUnicodeCharacter{2514}{\rule[.2\baselineskip]{.4pt}{.5\baselineskip}%
  \rule[.2\baselineskip]{1ex}{.4pt}}
\begin{document}

\begin{verbatim}
█ [1:0x561b3f574358] <list>
├─[2:0x561b3e9cd938] <dbl>
├─[3:0x561b3e9cd900] <dbl>
└─[4:0x561b3e9cd8c8] <dbl>
\end{verbatim}
\end{document}

使用 PDFLaTeX

答案2

您可以使用pmboxdraw,但是块太宽,因此我将\textblock(对应 U+2588)替换为\textlfblock(对应 U+258C),并将宽度限制为 0.5em。

\documentclass{article}
\usepackage{pmboxdraw}

\DeclareUnicodeCharacter{2588}{\makebox[0.5em][l]{\textlfblock}}

\begin{document}

\begin{verbatim}
█ [1:0x561b3f574358] <list>
├─[2:0x561b3e9cd938] <dbl>
├─[3:0x561b3e9cd900] <dbl>
└─[4:0x561b3e9cd8c8] <dbl>
\end{verbatim}

\end{document}

在此处输入图片描述

答案3

除了已经解决的主要问题(渲染一些符号)之外,您可能还想探索其他替代方案来逐字渲染目录树。这里只是使用旧版本dirtree和更通用的版本获得类似结果的比较forest

姆韦

% Use this with XeLaTeX or LuaLaTeX not PDFLaTeX.
\documentclass{article}
\usepackage{libertinus}
\usepackage[margin=1cm]{geometry}
\usepackage{dirtree}
\usepackage[edges]{forest}
\usepackage{multicol}

\begin{document}
\begin{multicols}{3}
\section{verbatim}
\begin{minipage}{\linewidth}
\begin{verbatim}
█ [1:0x561b3f574358] <list>
├─[2:0x561b3e9cd938] <dbl>
├─[3:0x561b3e9cd900] <dbl>
└─[4:0x561b3e9cd8c8] <dbl>
\end{verbatim}
\end{minipage}
\newcolumn

\section{dirtree}
\begin{minipage}{\linewidth}
\setlength{\DTbaselineskip}{12pt}        % optional
\DTsetlength{1pt}{6pt}{5pt}{.4pt}{.4pt} % optional
\ttfamily
\dirtree{%
.1 \rule[-.35em]{11.6pt}{1.5em} 
[1:0x561b3f574358] <list>. 
.2 [2:0x561b3e9cd938] <dbl>. 
.2 [3:0x561b3e9cd900] <dbl>. 
.2 [4:0x561b3e9cd8c8] <dbl>. 
}
\end{minipage}
\newcolumn

\section{forest}
\begin{minipage}{\linewidth}
\begin{forest}
forked edges, for tree={ grow'=0, folder, font=\ttfamily, s sep=1pt
}
[\mbox{[1:0x561b3f574358] <list>}, fill=gray!10, draw
[\mbox{[2:0x561b3e9cd938] <dbl>}]
[\mbox{[3:0x561b3e9cd900] <dbl>}]
[\mbox{[4:0x561b3e9cd8c8] <dbl>}]
]
\end{forest}
\end{minipage}
\end{multicols}
\end{document}

相关内容