### Effect of N type and concentration on C_{1−x}N_{x} stability

Generally, there are three bonding configurations that appear in the NG lattice. As described by Wang et al.^{17}: (1) a graphitic N atom is an N atom that replaces a C atom in the graphene lattice and bonds with three other adjacent C atoms in the hexagonal ring; (2) a pyridinic N bonds with two C atoms at the edges or defects of graphene and contributes one p electron to the π system; and (3) a pyrrolic N contributes two p electrons to the π system, though this is not restricted to a five-membered ring, as in pyrrole. Comparing these nitrogen types, pyridinic N and graphitic N are *sp*^{2} hybridized and pyrrolic N is *sp*^{3} hybridized. To determine the probability of producing different N configurations in NG, we calculated their formation energy per N atom. Firstly, we define the total formation energy of the structure, *E*_{f}(C_{1−x}N_{x}; *Gr*, N_{2}) as

$$E_f({rm{C}}_{1 – x}{rm{N}}_x;Gr,{rm{N}}_2) = E_{{rm{C}}_{1 – x}{rm{N}}_x} – (1 – x)mu _{C(Gr)} – xmu _{{rm{N}}({rm{N}}_2)},$$

(1)

where (E_{{rm{C}}_{1 – x}{rm{N}}_x}) is the free energy of C_{1−x}N_{x}, (mu _{{rm{C}}(Gr)}) is the chemical potential of C in perfect graphene and (mu _{{rm{N}}({rm{N}}_2)}) is the chemical potential of N in N_{2} (for details see Supplementary Note 1). In general, this is dependent on the temperature and partial pressure of N_{2}, but we consider the system at 0 K first. Figure 1 shows the calculated values of (E_f({rm{C}}_{1 – x}{rm{N}}_x;Gr,{rm{N}}_2)) for various values of *x* at 0 K. In each structure, the doping percentage of N is limited to less than 1 at% (*x* < 0.01) to minimize the interactions between the neighboring N dopants. Figure 1 shows that graphitic and pyridinic N in a hexagonal pore have much lower formation energies (~3 eV per N atom) than the pyrrolic pore (~1 eV per N atom). The lower formation energies indicate that graphitic and pyridinic N have higher probability of forming (based on thermodynamic considerations), therefore only graphitic and pyridinic N in a hexagonal pore (pyri-N-3) were considered in the following NG structural search.

Graphitic N–N pairs in NG are reported to be unfavorable due to the strong electrostatic repulsion between N atoms^{13}. This effect also prevents complete nitrogen–carbon phase separation and leads to the formation of carbon-nitride materials. To search for the most stable structures of graphitic NG (C_{1−x}N_{x}) in various N concentration ranges, we calculated the nearest-neighbor interaction between N dopants. A 12 × 12 supercell of graphene is used to calculate the interaction between two graphitic N atoms in the results presented below. Larger supercell sizes were found to give similar results. As shown in the inset of Fig. 2a, the honeycomb lattice of graphene can be considered to consist of two interpenetrating triangular sublattices: sublattice A and sublattice B. We consider the interaction between an N atom at an arbitrarily chosen site (labeled as 0A) and those at other sites that have different interaction distances to the 0A N atom. The pair-interaction energy (*E*_{p}) between N atoms is calculated by Eq. (1), where each C_{1−x}N_{x} structure contains only two N atoms. The calculated values of *E*_{p} for N atoms within an interaction range of 6.5 Å are plotted in Fig. 2a and shown as a function of the N–N distance. It can be seen that *E*_{p} increases with the shortening of the N–N distance, especially when the distance is less than 3 Å (or 0A–3B in Fig. 2a). Two local minima, the 0A–3B and 0A–7B pairs, were identified in the calculated energy curve, which is consistent with a previous report which also found these two local minima^{13}.

Since there is no N–C phase separation in NG, it seems likely that we can construct C_{1−x}N_{x} superstructures with uniform distributions of N dopants. Figure 2b shows the calculated formation energies of the most stable structures of C_{1−x}N_{x} 2D materials with different N/C ratios. These structures and the values of the formation energies are shown in Supplementary Fig. 1. Figure 2b clearly shows that the formation energies of the C_{1−x}N_{x} materials increase with N concentration. The most stable structure of each C_{1−x}N_{x} (*x* < 0.25) is mainly composed of 0A–3B and 0A–7B N–N pairs that have low pair interaction energies (Supplementary Fig. 2), demonstrating that the N–N pair interaction plays an important role in determining the structures of C_{1−x}N_{x} (*x* < 0.25). For C_{1−x}N_{x} structures with *x* > 0.25, the formation energies increase dramatically with the N concentration because of the short distance and hence strong repulsion of the N–N pairs.

C_{1−x}N_{x} structures composed of pyridinic N are also considered in our calculations since pyridinic N has a similar formation energy to that of graphitic N at low doping concentration (Fig. 1). Unlike graphitic N-dopants, which preserve the honeycomb structure of graphene, doping of pyridinic N in graphene leads to porous C_{1−x}N_{x} structures where each edge of the pore is composed of N atoms. Figure 3a shows the formation energies of pyridinic N in hexagonal pores. To test for a pore-size effect, the number of N atoms at the edge is increased from 1 to 4. It is clear that the formation energy of pyridinic N increases with pore size, demonstrating that it is more energetically favorable to have pyridinic N in small pores. Using the smallest hexagonal pore as a building block, we can construct C_{1−x}N_{x} superstructures with a uniform distribution of hexagonal pores. As shown in Fig. 3b, a series of superstructures with different pore densities are constructed by reducing the distance between the pores. This also leads to an increase in the N concentration. From the calculated energy curve of Fig. 3b, we can clearly see that the formation energies of the porous structures increase with N concentration. This prevents the formation of large pores and produces graphite materials with pyridinic N structures that have uniformly spaced small pores. It should be noted that only C_{1−x}N_{x} structures with N concentrations up to 0.5 are considered, since the structures with higher N concentration involve both pyridinic and graphitic N and will be discussed later.

To understand the formation probability of observing graphitic and pyridinic N in NG, we compared the formation energies of the most stable C_{1−x}N_{x} structures with graphitic or pyridinic N. The N concentration of the C_{1−x}N_{x} structures was varied from 0 to 0.5. The local PSO algorithm was used to search all the stable structures of 2D C_{1−x}N_{x} (Supplementary Fig. 4). It is worth noting that the structure search for hexagonal pyridinic N with very low N-doping concentration requires large lattice parameters, which is not easily achievable using PSO algorithm searching. Therefore, all the pyridinic N structures with low N concentration were constructed manually and optimized using DFT calculations (Supplementary Fig. 5). Figure 4a and b show the formation energies and the most stable structures with graphitic N or pyridinic N in hexagonal pores. It shows that graphitic N and pyridinic N have formation energies that are within ~0.1 eV atom^{−1} at very low N-doping concentrations (<0.08, which is shown as Region-I in Fig. 4a). When the doping concentration of N increases (Region-II shown in Fig. 4a), the pyridinic NG becomes more energetically favorable than graphitic NG. This means the increased N-doping concentration can increase the formation probability of pyridinic N. When the N concentration is higher than 0.25 (Region-III shown in Fig. 4a), the energy difference between graphitic and pyridinic N becomes much larger (>0.1 eV atom^{−1}), which means the C_{1−x}N_{x} structures with high N concentration predominantly contain pyridinic N. Interestingly, further calculations demonstrate that the coexistence of pyridinic and graphitic N in 2D C_{1−x}N_{x} decreases the formation energies of these structures (Supplementary Figs. 6 and 7). In particular, we found that g-C_{3}N_{4} (*x* = 0.57), which consists of pyridinic and graphitic N (Supplementary Fig. 8), has lower formation energy than CN (*x* = 0.50) which is composed of pyridinic N only, demonstrating that co-doping of pyridinic and graphitic N in 2D C_{1−x}N_{x} is beneficial to the energetic stability of the structure. This is consistent with numerous previous experimental observations that show pyridinic and graphitic N coexist in NG with low doping concentrations^{7,18,19} (Supplementary Table 1), and particularly with the stability of experimentally synthesized g-C_{3}N_{4}, which contains both graphitic and pyridinic N^{18}.

Figure 4a also demonstrates that formation of NG with low doping concentration is more thermodynamically favorable than those with high doping concentrations, and this is consistent with the fact that the experimentally synthesized NGs typically have very low N-doping concentrations^{19}.

Of all the structures considered, C_{3}N (*x* = 0.25 for graphitic N structures) demonstrate enhanced stability because their formation energies are the local minimum of the energy curves. The enhanced stability of C_{3}N composed of graphitic N can be attributed to the existence of 0A–3B N–N pairs, which we have demonstrated to be stable (Supplementary Fig. 2), and the high symmetry of the structure^{13}. It should be noted that the graphitic N-doped C_{3}N (*x* = 0.25) has already been synthesized and characterized in recent experiments^{20,21}. Experimentally, pyrrolic N has also been widely observed in NG as a defect. To compare with experiments, we also considered the effect of pyrrolic N on the formation energy of NG structures. It is found that the existence of a small concentration of pyrrolic N in NG greatly increases the formation energy of the 2D C_{1−x}N_{x} structures. (Supplementary Figs. 6 and 7). The addition of pyrrolic N into the NG structures containing only pyridinic N or both pyridinic and graphitic N leads to an increase of the formation energy. This demonstrates that pyrrolic N is not energetically favorable in NG, and the experimentally observed pyrrolic N in NG may be attributed to the formation of pyrrolic N at the edges of the structures. Moreover, we studied the formation of pyrrolic N in the experimentally synthesized C_{2}N (Supplementary Fig. 9) and g-C_{3}N_{4} (Supplementary Fig. 10). Similarly to the 2D C_{1−x}N_{x} structures discussed above, the formation energies of both C_{2}N and g-C_{3}N_{4} increase with addition of pyrrolic N defects. Considering systems with the same proportion of pyrrolic N defects, but in different positions, C_{2}N (or g-C_{3}N_{4}) with large distances between the defects leads to a lower formation energy of the structure. (Supplementary Figs. 9 and 10). Furthermore, for g-C_{3}N_{4} with high defect concentrations of pyrrolic N, the strain introduced by addition of the defects is so high that it causes the C–N bond to break in the calculations, and this reduces the formation energy of the system (Supplementary Fig. 10).

### Regulation of the structure of C_{1−x}N_{x}

The calculations of formation energies above are with reference to *μ*_{C(Gr)} for C in graphene and (mu _{{rm{N}}({rm{N}}_2)}) for N in N_{2} at 0 K. Since the chemical potential of C and N will vary with temperature, partial pressure of the feedstock and the type of feedstock, the formation free energies can be tuned by changing these parameters^{22,23}. A general expression for the formation energy is given by Eq. (S20) of Supplementary Note 1. Figure 5a and b show how the formation free energies of NG change with change in *μ*_{C} and *μ*_{N} due to change in the feedstock, at fixed temperature and pressure. Figure 5a shows the results obtained when *μ*_{C} is fixed at the chemical potential of a C atom in graphene at 0 K and *μ*_{N} is increased from 0 to 1.0 eV. In this case, the formation energies of the C_{1−x}N_{x} structures decrease, and the effect is greatest when the structures have high N concentrations as the formation energy is dependent on *x* (Eq. (1)). This means that C_{1−x}N_{x} structures with high N concentration become more energetically favorable than those with low N concentration when the chemical potential changes by about 0.5 eV. However, if *μ*_{N} is kept constant at the chemical potential of an N atom in N_{2} and *μ*_{C} is increased (Fig. 5b), the formation energies of C_{1−x}N_{x} structures decrease, but structures with low N concentrations have a more substantial reduction in energy. Therefore, C_{1−x}N_{x} structures with low N-doping concentration become much more energetically favorable than structures with high N-doping concentration, which finally leads to the production of C_{1−x}N_{x} materials with a higher percentage of graphitic N. To increase the proportion of C_{1−x}N_{x} materials with higher concentrations of N, a feedstock with a lower value of *μ*_{C} than graphene would need to be selected.

In experiments, the chemical potential of C and N atom in different feedstocks (defined in Eqs. (S2) and (S3) of Supplementary Information) can be calculated at different temperatures and pressures. The chemical potential of C in C_{m}H_{n} (*m* and *n* are integers), (mu _{{rm{C}}({rm{C}}_m{rm{H}}_n)}), is given by

$$mu _{{rm{C}}({rm{C}}_m{rm{H}}_n)}(T,P) = mu _{{rm{C}}(Gr)}(0) + {Delta}_TG_{{rm{C}}(Gr)}(T,P^0) + frac{1}{m}{Delta}_fG_{{rm{C}}_m{rm{H}}_n}^0(T) + frac{1}{m}RTln left[ {frac{{P_{{rm{C}}_m{rm{H}}_n}}}{{P^0}}left( {frac{{P^0}}{{P_{{rm{H}}_2}}}} right)^{n/2}} right],$$

(2)

where *G* is the free energy, *T* is the temperature, *P*_{X} is the partial pressure of *X*, *P*^{0} = 0.1 MPa is the standard pressure, ({Delta}_fG_X^0(T)) is free energy of formation of *X* at standard pressure, and ({Delta}_TG_X(T) equiv G_X(T) – G_X(0)), (see Supplementary Information).

Similarly, (mu _{{rm{N}}({rm{N}}_k{rm{H}}_l)}) is given by

$$mu _{{rm{N}}({rm{N}}_k{rm{H}}_l)}(T,P) = mu _{{rm{N}}({rm{N}}_2)}(0,P^0) + {textstyle{1 over 2}}{Delta}_TG_{{rm{N}}_2}(T,P^0) + frac{1}{k}{Delta}_fG_{{rm{N}}_k{rm{H}}_l}^0(T) + frac{1}{k}RTln left[ {frac{{P_{{rm{N}}_k{rm{H}}_l}}}{{P^0}}left( {frac{{P^0}}{{P_{{rm{H}}_2}}}} right)^{l/2}} right].$$

(3)

The differences in chemical potential of C and N in the feedstock and that in graphene or nitrogen at 0 K can be defined as

$${Delta}mu _{{rm{C}}({rm{C}}_m{rm{H}}_n)}(T,P) equiv mu _{{rm{C}}({rm{C}}_m{rm{H}}_n)}(T,P) – mu _{{rm{C}}(Gr)}(0),$$

(4)

$${Delta}mu _{{rm{N}}({rm{N}}_k{rm{H}}_l)}(T,P) equiv mu _{{rm{N}}({rm{N}}_k{rm{H}}_l)}(T,P) – mu _{{rm{N}}({rm{N}}_2)}(0),$$

(5)

respectively, and values 0 K for a number of feedstocks are given in Supplementary Table 2. Using these definitions, the free energy for the formation of C_{1−x}N_{x} at temperature of *T*, and pressure of *P*, from C_{m}H_{n} and N_{k}H_{l}, *E*_{f}(C_{1−x}N_{x}; C_{m}H_{n}, N_{k}H_{l}, *T*, *P*), can be expressed as

$$begin{array}{l}E_f({rm{C}}_{1 – x}{rm{N}}_x;{rm{C}}_m{rm{H}}_n,{rm{N}}_k{rm{H}}_l,T,P)\ = E_f({rm{C}}_{1 – x}{rm{N}}_x;Gr,{rm{N}}_2,0) – (1 – x){Delta}mu _{{rm{C}}({rm{C}}_m{rm{H}}_n)}(T,P) – x{Delta}mu _{{rm{N}}({rm{N}}_k{rm{H}}_l)}(T,P) + ,{Delta}_TG_{{rm{C}}_{1 – x}{rm{N}}_x}(T),end{array}$$

(6)

where we assume that the thermal contribution to the free energy of carbon in graphene equals the thermal contribution to the free energy of C_{1−x}N_{x} (details can be found in Supplementary Information).

Based on the calculated Δ*μ*_{C} and Δ*μ*_{N} in different feedstocks with typical experimental temperatures and pressures (Supplementary Table 3, *T* = 1300 K, *P*_{0} = 0.1 MPa, (P_{{rm{H}}_2} = P_{rm{feedstock}} = 10^{ – 6},{mathrm{MPa}})), the formation energies of C_{1−x}N_{x} can be calculated. Figure 5c shows the effect of different N feedstocks, when CH_{4} is used as the C feedstock. The N feedstocks considered are N_{2}, NH_{3}, and N_{2}H_{4}. It is clear that N_{2} favors the formation of C_{1−x}N_{x} with low N concentration, whereas N_{2}H_{4} enhances the formation probability of C_{1−x}N_{x} with high N concentration. Figure 5d shows the effect of different C feedstocks, when NH_{3} is used as the N feedstock. The C feedstocks considered are CH_{4}, C_{2}H_{4}, and C_{2}H_{2}. We can see that use of C_{2}H_{2} makes the free energy curve flatter than with CH_{4} or C_{2}H_{4}. This means the formation of C_{1−x}N_{x} with high N concentration and pyridinic N is more likely in this case. These predictions are consistent with previous experimental observations. For example, Wei et al. found that using CH_{4} and NH_{3} as the feedstock, the synthesized NGs have both pyridinic and graphitic N^{24}, while Luo et al. observed the synthesized NGs were composed purely of pyridinic N by changing the feedstock to C_{2}H_{4} and NH_{3}^{25}.

Equations (2) and (3) demonstrate that the growth temperature can also be used to tune the structures of C_{1−x}N_{x} by changing the chemical potential of C and N. This is demonstrated in Fig. 5e where the effect of temperature on a system with CH_{4} and NH_{3} feedstock and typical experimental pressures is shown. Higher temperatures lead to the production of C_{1−x}N_{x} with an increased percentage of graphitic N and decreased N-doping concentration. This has been observed in a previous research where the percentage of graphitic N increases with the temperature, and the N concentration of the NG decreases with the temperature^{26,27}. For example, Zhang et al. and Guo et al. synthesized NG with high N concentration and quite precise doping at low temperatures, and they found the samples contain abundant pyridinic N^{28,29}. The pressure also has significant effect on the formation energies of the C_{1−x}N_{x} structures. Compared with the case where the partial pressures of H_{2}, C and N feedstocks are fixed as 10^{−5} bar (black line + circle in Fig. 5f), decreasing the pressure ratio of C feedstock and H_{2} ((P_{rm{C}}/P_{{rm{H}}_2}) = 0.1, blue line + triangular in Fig. 5f) results in smaller slope of the line, which means the formation of C_{1−x}N_{x} structures with high N-doping concentration and more pyridinic N. However, increasing the pressure ratio of C feedstock and H_{2} ((P_{rm{C}}/P_{{rm{H}}_2} equiv P_{{rm{C}}_m{rm{H}}_n}/P_{{rm{H}}_2} = 10), blue line + pentagon in Fig. 5f)) promotes the formation of C_{1−x}N_{x} structures with low N-doping concentration and more graphitic N. In contrast, decreasing the pressure ratio of N feedstock and H_{2} ((P_{rm{N}}/P_{{rm{H}}_2} equiv P_{{rm{N}}_k{rm{H}}_l}/P_{{rm{H}}_2} = 0.1), red line + rectangular in Fig. 5f) favors the formation of C_{1−x}N_{x} structures with low N-doping concentration and more graphitic N, while increasing the pressure ratio of N feedstock and H_{2} ((P_{rm{N}}/P_{{rm{H}}_2} = 10), red line + star in Fig. 5f) favors the formation of C_{1−x}N_{x} structure with high N-doping concentration and more pyridinic N.

### Electronic properties of C_{1−x}N_{x}

The structure of 2D C_{1−x}N_{x} has important effect on its properties. The electronic properties of graphitic NG and pyridinic NG were studied by both Perdew–Burke–Ernzerhof (PBE) and HSE06 calculations. For 2D C_{1−x}N_{x} structures with graphitic N, most structures are metallic except C_{12}N and C_{3}N, which are semiconductors with medium band gaps (Supplementary Fig. 11). The 2D C_{1−x}N_{x} structures with pyridinic N, C_{12}N, C_{8}N, C_{6}N, and C_{10}N_{3} have the special graphene-like Dirac cone band structures, while the other structures are semiconductors with a direct bandgap (Supplementary Fig. 12). To obtain more accurate bandgaps for the C_{1−x}N_{x} structures, the HSE06 functional was used to calculate the bandgaps of C_{12}N, C_{3}N, C_{2}N, and CN, of which C_{3}N and C_{2}N have been synthesized experimentally. Table 1 shows the calculated bandgaps for these materials together with the experimental results. The results of the HSE06 calculations indicate that C_{3}N (*x* = 0.25) composed of graphitic N has a bandgap of 1.22 eV. Experimentally, the measured bandgaps of C_{3}N are highly dependent on the flake size of the materials. When the size of C_{3}N quantum dots (QDs) varies from 1.8 to 5.5 nm, the bandgap changes from 2.74 to 1.57 eV^{20,21}. The calculated bandgap of C_{2}N (*x* = 0.33), which is composed of pyridinic N, is 2.46 eV, and the experimentally measured bandgaps are between 1.69 and 2.8 eV when the size of C_{2}N QDs changes from 3.8 to 4.8 nm^{30,31}. The HSE06 calculations demonstrate that C_{12}N (*x* = 0.08) composed of graphitic N has a bandgap of 0.99 eV and CN (*x* = 0.50) composed of pyridinic N has a bandgap of 3.46 eV (Table 1 and Supplementary Fig. 13).

In addition, we found the carrier mobilities, μ_{2D}, of structures composed of graphitic N (e.g., C_{3}N) are much higher than the structures composed of pyridinic N (e.g., C_{2}N), (see Supplementary Note 2, Table 2, and Supplementary Fig. 14). These studies demonstrate that controlling the structure of C_{1−x}N_{x} is an important way to obtain carbon-nitride materials with desired properties.

In conclusion, by comparing the formation energies of graphitic and pyridinic N under different N concentrations, we found that graphitic and pyridinic N have similar formation energies in structures with low N-doping concentration. However, increasing the N-doping concentration leads to a high probability of formation of C_{1−x}N_{x} composed of purely pyridinic N. The low N-doping concentration of graphene in previous experiments can be attributed to the low formation energies of NG with low N concentration. Our theoretical calculations demonstrate that the doping concentration can be tuned by choosing the feedstock and growth temperature and pressure.