0

I am having some problems when formatting c++ code using listings. Here is my WME:

\documentclass{standalone}
\usepackage[left=1.00in, right=1.00in, top=1.00in, bottom=1.00in]{geometry}
\usepackage{tcolorbox,listings}
\usepackage{listings}
\usepackage{tikz}
\usepackage{framed}
\usepackage{minted}

\definecolor{background}{HTML}{EEEEEE}
\definecolor{comments}{HTML}{868686}
\definecolor{darkred}{RGB}{139,0,0}
\definecolor{darkblue}{RGB}{0,0,139}
\definecolor{chartreuse}{RGB}{127,255,0}
\definecolor{drakgreen}{RGB}{0,128,0}
\definecolor{lightgray}{RGB}{238,239,240}

\lstset{
    language=[LaTeX]Tex,
    keywordstyle=\color{darkblue},
    texcsstyle=*\color{blue},
    basicstyle=\normalfont\ttfamily,
    commentstyle=\color{comments}\ttfamily,
    stringstyle=\rmfamily,
    numbers=none,
    showstringspaces=false,
    breaklines=true,
    frameround=ftff,
    captionpos=t,
    belowcaptionskip=0em,
    belowskip=0em,
}
\lstdefinestyle{mystyle}{
     language=C++,
     extendedchars=true, 
     breaklines=true,
     breakatwhitespace=true,
     basicstyle=\ttfamily,
     keywordstyle=\color{darkblue},
     keywordstyle=[2]\color{blue},
     keywordstyle=[3]\color{darkblue},
     keywordstyle=[4]\color{drakgreen},
     alsoletter = {!},
     keywords=[2]{cout,cin},
 }

\tcbuselibrary{listings,skins,breakable}
\newtcblisting{code}{
      arc=0mm,
      top=0mm,
      bottom=0mm,
      left=3mm,
      right=0mm,
      width=\textwidth,
      boxrule=1pt,
      colback=lightgray,
      listing only,
      listing options={style=mystyle},
      breakable
}

\begin{document}
\begin{code}
/*
 * C++ Program to Implement The Edmonds-Karp Algorithm
 */
#include<cstdio>
#include<cstdio>
#include<queue>
#include<cstring>
#include<vector>
#include<iostream>
#include<conio.h>

using namespace std;

int capacities[10][10];
int flowPassed[10][10];
vector<int> graph[10];
int parentsList[10];
int currentPathCapacity[10];

int bfs(int startNode, int endNode)
{
    memset(parentsList, -1, sizeof(parentsList));
    memset(currentPathCapacity, 0, sizeof(currentPathCapacity));

    queue<int> q;
    q.push(startNode);

    parentsList[startNode] = -2;
    currentPathCapacity[startNode] = 999;

    while (!q.empty())
    {
        int currentNode = q.front();
        q.pop();

        for (int i = 0; i < graph[currentNode].size(); i++)
        {
            int to = graph[currentNode][i];
            if (parentsList[to] == -1)
            {
                if (capacities[currentNode][to] - flowPassed[currentNode][to] > 0)
                {
                    parentsList[to] = currentNode;
                    currentPathCapacity[to] = min(currentPathCapacity[currentNode],capacities[currentNode][to] - flowPassed[currentNode][to]);
                    if (to == endNode)
                    {
                        return currentPathCapacity[endNode];
                    }
                    q.push(to);
                }
            }
        }
    }
    return 0;
}

int edmondsKarp(int startNode, int endNode)
{
    int maxFlow = 0;
    while (true)
    {
        int flow = bfs(startNode, endNode);
        if (flow == 0)
        {
            break;
        }
        maxFlow += flow;
        int currentNode = endNode;
        while (currentNode != startNode)
        {
            int previousNode = parentsList[currentNode];
            flowPassed[previousNode][currentNode] += flow;
            flowPassed[currentNode][previousNode] -= flow;
            currentNode = previousNode;
        }
    }
    return maxFlow;
}
int main()
{
    int nodesCount, edgesCount;
    cout << "enter the number of nodes and edges\n";
    cin >> nodesCount >> edgesCount;

    int source, sink;
    cout << "enter the source and sink\n";
    cin >> source >> sink;

    for (int edge = 0; edge < edgesCount; edge++)
    {
        cout << "enter the start and end vertex alongwith capacity\n";
        int from, to, capacity;
        cin >> from >> to >> capacity;

        capacities[from][to] = capacity;
        graph[from].push_back(to);

        graph[to].push_back(from);
    }

    int maxFlow = edmondsKarp(source, sink);

    cout << endl << endl << "Max Flow is:" << maxFlow << endl;

    getch();
}
\end{code}

\end{document}

enter image description here

Two problems can be seen from the output, excessive indentation and no automatic line breaks. Where is the problem?

0

1 Answer 1

2

Okay, so here's a tidied version of your code:

  • You do not need packages like minted if you are typesetting with listings.
  • Your code is indented with spaces, so every space in the source is translated into one space in the output. Simply running a search & replace will "fix" the excessive space. If you use "real" tabs you might want to specify tabsize.
  • Using the standalone class is a bad idea if you want to show breakable behavior and fixed width (as it adjusts the page size to the content).
  • You are only specifying breaklines,breakatwhitespace=true. Hence, your long line calling min will not break correctly as there is no whitespace to break hat. You might want to introduce a space after commas.

Code:

\documentclass{article}
\usepackage[left=1.00in, right=1.00in, top=1.00in, bottom=1.00in]{geometry}
\usepackage[listings,skins,breakable]{tcolorbox}

\definecolor{darkred}{RGB}{139,0,0}
\definecolor{darkblue}{RGB}{0,0,139}
\definecolor{darkgreen}{RGB}{0,128,0}
\definecolor{lightgray}{RGB}{238,239,240}

\lstdefinestyle{mystyle}{
     language=C++,
     extendedchars=true, 
     breaklines=true,
     breakatwhitespace=true,
     basicstyle=\ttfamily,
     keywordstyle=\color{darkblue},
     keywordstyle=[2]\color{blue},
     keywordstyle=[3]\color{darkblue},
     keywordstyle=[4]\color{darkgreen},
     alsoletter = {!},
     tabsize=2,
     keywords=[2]{cout,cin},
 }

\newtcblisting{code}{
      arc=0mm,
      top=0mm,
      bottom=0mm,
      left=3mm,
      right=0mm,
      width=\textwidth,
      boxrule=1pt,
      colback=lightgray,
      listing only,
      listing options={style=mystyle},
      breakable
}

\begin{document}
\begin{code}
/*
 * C++ Program to Implement The Edmonds-Karp Algorithm
 */
#include<cstdio>
#include<cstdio>
#include<queue>
#include<cstring>
#include<vector>
#include<iostream>
#include<conio.h>

using namespace std;

int capacities[10][10];
int flowPassed[10][10];
vector<int> graph[10];
int parentsList[10];
int currentPathCapacity[10];

int bfs(int startNode, int endNode)
{
  memset(parentsList, -1, sizeof(parentsList));
  memset(currentPathCapacity, 0, sizeof(currentPathCapacity));

  queue<int> q;
  q.push(startNode);

  parentsList[startNode] = -2;
  currentPathCapacity[startNode] = 999;

  while (!q.empty())
  {
    int currentNode = q.front();
    q.pop();

    for (int i = 0; i < graph[currentNode].size(); i++)
    {
      int to = graph[currentNode][i];
      if (parentsList[to] == -1)
      {
        if (capacities[currentNode][to] - flowPassed[currentNode][to] > 0)
        {
          parentsList[to] = currentNode;
          currentPathCapacity[to] = min(currentPathCapacity[currentNode], capacities[currentNode][to] - flowPassed[currentNode][to]);
          if (to == endNode)
          {
            return currentPathCapacity[endNode];
          }
          q.push(to);
        }
      }
    }
  }
  return 0;
}

int edmondsKarp(int startNode, int endNode)
{
  int maxFlow = 0;
  while (true)
  {
    int flow = bfs(startNode, endNode);
    if (flow == 0)
    {
      break;
    }
    maxFlow += flow;
    int currentNode = endNode;
    while (currentNode != startNode)
    {
      int previousNode = parentsList[currentNode];
      flowPassed[previousNode][currentNode] += flow;
      flowPassed[currentNode][previousNode] -= flow;
      currentNode = previousNode;
    }
  }
  return maxFlow;
}
int main()
{
  int nodesCount, edgesCount;
  cout << "enter the number of nodes and edges\n";
  cin >> nodesCount >> edgesCount;

  int source, sink;
  cout << "enter the source and sink\n";
  cin >> source >> sink;

  for (int edge = 0; edge < edgesCount; edge++)
  {
    cout << "enter the start and end vertex alongwith capacity\n";
    int from, to, capacity;
    cin >> from >> to >> capacity;

    capacities[from][to] = capacity;
    graph[from].push_back(to);

    graph[to].push_back(from);
  }

  int maxFlow = edmondsKarp(source, sink);

  cout << endl << endl << "Max Flow is:" << maxFlow << endl;

  getch();
}
\end{code}

\end{document}

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .