![乳胶中的断裂会产生额外的标签](https://linux22.com/image/342815/%E4%B9%B3%E8%83%B6%E4%B8%AD%E7%9A%84%E6%96%AD%E8%A3%82%E4%BC%9A%E4%BA%A7%E7%94%9F%E9%A2%9D%E5%A4%96%E7%9A%84%E6%A0%87%E7%AD%BE.png)
我正在尝试学习 Latex,我必须解决一个涉及 BNF 语法规则的作业。
这是我的代码:
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{syntax}
\title{Hello world }
\author{Buk Lau }
\date{October, 28 2016}
\usepackage{natbib}
\usepackage{graphicx}
\begin{document}
\maketitle
\section{Second Problem}
We have the following BNF grammar rule:
\begin{grammar}
<pop> ::= [<bop> , <pop>] | <bop>\\
<bop> ::= <boop> | (<pop> )\\
<boop> ::= x|y|z
\end{grammar}
\section{Conclusion}
``I always thought something was fundamentally wrong with the universe'' \citep{adams1995hitchhiker}
\bibliographystyle{plain}
\bibliography{references}
\end{document}
这是我的输出:
编译时没有出现任何错误。但是,在 break 之后(接下来的两个语句中)添加了一个额外的制表符:
为什么会发生这种情况?我该怎么做才能解决这个问题?此外,如何将这三个语句集中起来(这实际上不是这个问题的一部分,但如果你能告诉我怎么做,我将不胜感激)?
答案1
包装手册texdoc syntax
指出,单独的生产规则应该用空行分隔,并且这\\
是为了让长规则在一行上持续(因此缩进)
\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{syntax}
\title{Hello world }
\author{Buk Lau }
\date{October, 28 2016}
\usepackage{natbib}
\usepackage{graphicx}
\begin{document}
\maketitle
\section{Second Problem}
We have the following BNF grammar rule:
\begin{grammar}
<pop> ::= [<bop> , <pop>] | <bop>
<bop> ::= <boop> | (<pop> )
<boop> ::= x|y|z
\end{grammar}
\section{Conclusion}
``I always thought something was fundamentally wrong with the universe'' \citep{adams1995hitchhiker}
\bibliographystyle{plain}
\bibliography{references}
\end{document}
答案2
除了使用syntax
,您还可以通过以下方式定义类似的东西listings
。
有一些literate
变化可以改善布局,包括使用\vcentcolon
更好的预览::=
:
\documentclass{article}
\usepackage{listings,mathtools}
\lstnewenvironment{grammar}[1][]
{\lstset{
%basicstyle = \ttfamily,
numbers = none,
fontadjust = true,
flexiblecolumns = true,
keepspaces = false,
literate = {<}{$\langle$}1
{>}{$\rangle$}1
{::=}{$\vcentcolon\vcentcolon=$}3
}}
{}
\begin{document}
\noindent
We have the following BNF grammar rule:
\begin{grammar}
<pop> ::= [<bop> , <pop>] | <bop>
<bop> ::= <boop> | (<pop>)
<boop> ::= x|y|z
\end{grammar}
\end{document}