更新

更新

当我整合这个如何使用标准 -(连字符)作为 hyphenat 包中的 \hyp{} 命令?进入我的主文档,我收到有关翻译包中连字符使用的错误:

Redoing nameref's sectioning
Redoing nameref's label
(D:\User\Documents\latex\texmfs\install\tex\latex\microtype\mt-cmr.cfg) (D:\User\Documents\latex\texmfs\install\tex\context\base\supp-pdf.mkii
[Loading MPS to PDF converter (version 2006.09.02).]
) ABD: EverySelectfont initializing macros (D:\User\Documents\latex\texmfs\install\tex\latex\beamer\translator\dicts\translator-basic-dictionary\translator-basic-dictionary-English.dict
D:\User\Documents\latex\texmfs\install\tex\latex\beamer\translator\dicts\translator-basic-dictionary\translator-basic-dictionary-English.dict:2: Missing \endcsname inserted
D:\User\Documents\latex\texmfs\install\tex\latex\beamer\translator\dicts\translator-basic-dictionary\translator-basic-dictionary-English.dict:2:  ==> Fatal error occurred, no output PDF file produced!
Transcript written on D:\thesis\setup\cache\main.log.
gross execution time: 8855 ms
user mode: 8625 ms, kernel mode: 125 ms, total: 8750

... on main.log
Package lastpage Info: Please have a look at the pageslts package at
(lastpage)             https://www.ctan.org/pkg/pageslts
(lastpage)             ! on input line 332.
\c@lstlisting=\count631
 (D:\User\Documents\latex\texmfs\install\tex\latex\beamer\translator\dicts\translator-basic-dictionary\translator-basic-dictionary-English.dict
Dictionary: translator-basic-dictionary, Language: English 

D:\User\Documents\latex\texmfs\install\tex\latex\beamer\translator\dicts\translator-basic-dictionary\translator-basic-dictionary-English.dict:2: Missing \endcsname inserted 
Here is how much of TeX's memory you used:

然后,我将其像这样包含在我的文档中:

\begin{document}

    \ExplSyntaxOn
    \cs_new:Npn \hyphenfix_emdash:c {---}
    ...

    Tests.

    Encoding-encoding-encoding-encoding-encoding-encoding-encoding-encoding-encoding-encoding.

\end{document}

而且它成功了!

有没有办法将其包含在我的序言中而不是文档的开头?

更新

根据评论所建议,我添加了\AtBeginDocument{,但它不起作用:

\usepackage{expl3}
\AtBeginDocument{%
    \ExplSyntaxOn
    \cs_new:Npn \hyphenfix_emdash:c {---}
    \cs_new:Npn \hyphenfix_endash:c {--}

    \cs_new:Npn \hyphenfix_discardnext:NN #1#2{#1}


    \catcode`\-=\active

    \cs_new_protected:Npn -{
        \futurelet\hyphenfix_nexttok\hyphenfix_i:w
    }

    \cs_new:Npn \hyphenfix_i:w {
        \cs_if_eq:NNTF{\hyphenfix_nexttok}{-}{
            %discard the next `-` token
            \hyphenfix_discardnext:NN{\futurelet\hyphenfix_nexttok\hyphenfix_ii:w}
        }{
            % from package hyphenat
            \hyp
        }
    }

    \cs_new:Npn \hyphenfix_ii:w {
        \cs_if_eq:NNTF{\hyphenfix_nexttok}{-}{
            \hyphenfix_discardnext:NN{\hyphenfix_emdash:c}
        }{
            \hyphenfix_endash:c
        }
    }
    \ExplSyntaxOff
}

现在,它引发另一个错误:

Package microtype Info: Loading generic protrusion settings for font family
(microtype)             `pcr' (encoding: T1).
(microtype)             For optimal results, create family-specific settings.
(microtype)             See the microtype manual for details.
main.tex:333: Missing $ inserted 
Here is how much of TeX's memory you used:

main.tex:333我已经上线了\begin{document}

相关内容