0

I want to know if there is a option to set all the whitespaces in my source code.

The whitespaces in my CPP code do not look uniform. Sometimes I leave a space before and after the = sign, sometimes I dont. The same with loops etc.

Is there any parameter that makes the whitespaces, formatting, more "uniform"

2
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.
    – Community Bot
    Nov 4, 2022 at 11:50
  • 3
    is this a question about tex typesetting, or a question about settings in a C++ source code editor? You have tagged listings and minted, which are you using? Nov 4, 2022 at 12:46

1 Answer 1

4

The listings and minted packages do not offer the functionality to reformat/prettify the code. However, a possible solution is to call an external formatter first and print the result. For C++ a formatter that handles spaces is clang-format, which is part of the Clang compiler suite.

From within LaTeX you can use the shellesc package for an easy cross-platform way of executing external programs. This package requires the shell-escape command line option, which is also needed for minted.

Example badly formatted C++ file:

#include <iostream>

int main(){
   int a= 3;
   int b =4;
   if( a==b){
      std::cout  << "no" <<std::endl;
      }else{ std::cout << "yes"<<std::endl;
      }
return 0;
}

LaTeX code:

\documentclass{article}
\usepackage{minted}
\usepackage{shellesc}

\newcommand{\reformatcpp}[1]{%
   \ShellEscape{clang-format #1 > #1.tmp}%
   \inputminted{c++}{#1.tmp}%
}

\begin{document}
\section*{Original file}
\inputminted{c++}{yourfile.cc}
\section*{Reformatted}
\reformatcpp{yourfile.cc}
\end{document}

Result:

enter image description here

You must log in to answer this question.

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