方程溢出过满 \hbox

方程溢出过满 \hbox

我对在 LateX 中显示 C++ 代码有疑问

当我执行以下操作时,C++ 会超出页面范围。有办法解决这个问题吗?

\documentclass{article} 

\newcommand{\dd}[1]{\mathrm{d}#1}
\usepackage[parfill]{parskip}
\usepackage{listings}
\usepackage{xcolor} % for setting colors
\usepackage{breqn}

% set the default code style
\lstset{
    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{green}, % comment color
    keywordstyle=\color{blue}, % keyword color
    stringstyle=\color{red} % string color
}

\begin{document}

\section{$\Delta A_2$ Derivation: Comparison with CRIS2}


First, let us start by showing the C++ code:
\begin{lstlisting}[language=C++, caption={C++ code using listings}]
{
  else if (evaltype == 5) 
  {
    for (std::size_t i=startp;i<p.size();++i) 
    {
      double & x = p[i].first; //input
      double & y = p[i].second; //result
      double q1 = rtx[8]-(cafdf(rtx[0],rtx[4]*x*x*x)-rtx[3])/rtx[1];
      double xi = crsxib(q1); 
      double p1i = 23.0/(1.0-xi)+ab[5]+xi*(11.0*ab[2]+xi*(2.0*ab[3]+xi*33.0*ab[4]));
      p1i *= 4.3723240245*ab[0];
      double si = (1.0+ci*(ab[99]+xi*(ab[44]+xi*ab[33])))/(3.0*(1.0-xi));
      y = p1i*xi*xy/abc[2]/dde[2]*(x/(1.0+dd*(x-1.0)))*(q/(1.0+si*(x-1.0)))*(x/(1.0+ci*(x-1.0)));
    }
  }
}
\end{lstlisting}

答案1

请在您的\lstset添加中breaklines = true,然后使用

\documentclass{article} 

\newcommand{\dd}[1]{\mathrm{d}#1}
\usepackage[parfill]{parskip}
\usepackage{listings}
\usepackage{xcolor} % for setting colors
\usepackage{breqn}

% set the default code style
\lstset{
frame=tlb, % 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{green}, % comment color
keywordstyle=\color{blue}, % keyword color
stringstyle=\color{red}, % string color
breaklines = true,
}

\begin{document}

\begin{lstlisting}[language=C++, caption={C++ code using listings}]
{
  else if (evaltype == 5) 
  {
    for (std::size_t i=startp;i<p.size();++i) 
    {
      double & x = p[i].first; //input
      double & y = p[i].second; //result
      double q1 = rtx[8]-(cafdf(rtx[0],rtx[4]*x*x*x)-rtx[3])/rtx[1];
      double xi = crsxib(q1); 
      double p1i = 23.0/(1.0-xi)+ab[5]+xi*(11.0*ab[2]+xi*(2.0*ab[3]+xi*33.0*ab[4]));
      p1i *= 4.3723240245*ab[0];
      double si = (1.0+ci*(ab[99]+xi*(ab[44]+xi*ab[33])))/(3.0*(1.0-xi));
      y = p1i*xi*xy/abc[2]/dde[2]*(x/(1.0+dd*(x-1.0)))*(q/(1.0+si*(x-1.0)))*(x/(1.0+ci*(x-1.0)));
    }
  }
}
\end{lstlisting}

\end{document}

你会看见

cpp 列表

相关内容