列表中的希腊语和英语

列表中的希腊语和英语

我正在尝试创建一个包含英文和希腊文字母的列表。

我已经设定了这个风格。

\lstset{
      basicstyle=\normalsize\selectlanguage{english}\ttfamily,  % COURIER NEW?
    frame=tb, % draw a frame at the top and bottom of the code block
    tabsize=4, % tab space width
    showstringspaces=false, % don't mark spaces in strings
    numbers=left, % display line numbers on the left
    commentstyle=\color{black}, % comment color
    keywordstyle=\color{black}, % keyword color
    stringstyle=\color{black} % string color
}

我想将此代码包含在 lstlisting 中。如您所见,其中有希腊文:“Σύνθετη Αναζήτηση”。但是,当我运行 pdfLatex 编译器时,它以拉丁字符呈现。

\begin{lstlisting}
<?php

function custom_toolbar_link($wp_admin_bar) {
    $args = array(
        'id' => 'advanced_search',
        'title' => 'Σύνθετη Αναζήτηση',
        'href' => 'admin.php?page=advanced-search',

        'meta' => array(
            'class' => 'advanced_search',
            'title' => 'advanced search'
        )
    );
    $wp_admin_bar->add_node($args);
}
add_action('admin_bar_menu', 'custom_toolbar_link', 10);
\end{lstlisting}

你能帮助我吗?

*** 文本的完整最小示例:

\documentclass[11pt,a4paper,fleqn]{article}
\usepackage{listings}
\usepackage{dirtree}
\usepackage{indentfirst}
\usepackage[greek,english]{babel}
\usepackage[iso-8859-7]{inputenc}
\usepackage{chicago}
\usepackage[section]{placeins}

\lstset{
      basicstyle=\normalsize\selectlanguage{english}\ttfamily,  % COURIER NEW?
    frame=tb, % draw a frame at the top and bottom of the code block
    tabsize=4, % tab space width
    showstringspaces=false, % don't mark spaces in strings
    numbers=left, % display line numbers on the left
    commentstyle=\color{black}, % comment color
    keywordstyle=\color{black}, % keyword color
    stringstyle=\color{black} % string color
}

% This concludes the preamble
\begin{document}

\begin{lstlisting}[label=some-code,caption=Some Code]
<?php
function custom_toolbar_link($wp_admin_bar) {
    $args = array(
        'id' => 'advanced_search',
        'title' => 'Σύνθετη Αναζήτηση',
        'href' => 'admin.php?page=advanced-search',
        'meta' => array(
            'class' => 'advanced_search',
            'title' => 'advanced search'
        )
    );
    $wp_admin_bar->add_node($args);
}
add_action('admin_bar_menu', 'custom_toolbar_link', 10);
\end{lstlisting}

\end{document}

答案1

如果您可以使用 LuaLaTeX 或 XeLaTeX,则可以通过fontspec其加载任何包含希腊语的等宽 Unicode 字体,例如 New Computer Modern Mono 或 Libertinus Mono。

\documentclass[11pt,a4paper,fleqn]{article}
\usepackage[english]{babel}
\usepackage{newcomputermodern}
\usepackage{listings}

\babelprovide[import]{greek}

\lstset{
    basicstyle=\normalsize\selectlanguage{english}\ttfamily,  % COURIER NEW?
    frame=tb, % draw a frame at the top and bottom of the code block
    tabsize=4, % tab space width
    showstringspaces=false, % don't mark spaces in strings
    numbers=left, % display line numbers on the left
    commentstyle=\color{black}, % comment color
    keywordstyle=\color{black}, % keyword color
    stringstyle=\color{black} % string color
}

% This concludes the preamble
\begin{document}

\begin{lstlisting}[label=some-code,caption=Some Code]
<?php
function custom_toolbar_link($wp_admin_bar) {
    $args = array(
        'id' => 'advanced_search',
        'title' => 'Σύνθετη Αναζήτηση',
        'href' => 'admin.php?page=advanced-search',
        'meta' => array(
            'class' => 'advanced_search',
            'title' => 'advanced search'
        )
    );
    $wp_admin_bar->add_node($args);
}
add_action('admin_bar_menu', 'custom_toolbar_link', 10);
\end{lstlisting}

\end{document}

您在评论中表示想要使用 Courier New。这不是我的建议,但如果您这样做,您可以添加以下行

\usepackage{fontspec}

\setmonofont{Courier New}[Scale=MatchLowercase]

如果您被迫使用 PDFLaTeX,该listing包不支持 UTF-8,但可能支持 ISO-8859-7。您可能希望使用它selinput来自动检测输入编码,以便保存为 UTF-8 的文档有机会进行编译。

\documentclass[11pt,a4paper,fleqn]{article}
\usepackage[LGR,T1]{fontenc}
\usepackage{selinput} % Auto-detect input encoding.
\usepackage{textalpha} % Enable Greek text
\usepackage[greek,english]{babel}
\usepackage{listings}

\SelectInputEncodingList{utf8, iso-8859-7}
\SelectInputMappings{ alpha=α }

\lstset{
    basicstyle=\normalsize\ttfamily,
    frame=tb, % draw a frame at the top and bottom of the code block
    tabsize=4, % tab space width
    showstringspaces=false, % don't mark spaces in strings
    numbers=left, % display line numbers on the left
    commentstyle=\color{black}, % comment color
    keywordstyle=\color{black}, % keyword color
    stringstyle=\color{black}, % string color
    extendedchars=false % listings does not support UTF-8.
}

% This concludes the preamble
\begin{document}

\begin{lstlisting}[label=some-code,caption=Some Code]
<?php
function custom_toolbar_link($wp_admin_bar) {
    $args = array(
        'id' => 'advanced_search',
        'title' => 'Σύνθετη Αναζήτηση'
        'href' => 'admin.php?page=advanced-search',
        'meta' => array(
            'class' => 'advanced_search',
            'title' => 'advanced search'
        )
    );
    $wp_admin_bar->add_node($args);
}
add_action('admin_bar_menu', 'custom_toolbar_link', 10);
\end{lstlisting}

\end{document}

另一个选择是使用minted

答案2

您可以转至 Latex。例如:

\begin{lstlisting}[label=some-code,caption=Some Code,escapeinside=;;]
<?php
function custom_toolbar_link($wp_admin_bar) {
$args = array(
'id' => 'advanced_search',
'title' => ';\selectlanguage{greek}Σύνθετη Αναζήτηση\selectlanguage{english};',
'href' => 'admin.php?page=advanced-search',
'meta' => array(
'class' => 'advanced_search',
'title' => 'advanced search'
)
);
$wp_admin_bar->add_node($args);
}
add_action('admin_bar_menu', 'custom_toolbar_link', 10);
\end{lstlisting}

相关内容