## Introduction

Due to its high carrier mobility of more than 1.5 × 104 cm2 V−1 s−1 at room temperature, graphene is a promising material for future high-speed transistors1. However, graphene field-effect transistors cannot be turned off effectively because they do not have a bandgap2. Heteroatom doping provides a promising approach to tune and control the electronic properties of graphene, in the same way that it is applied in electronics using silicon materials3,4,5. N atoms have similar atomic radii and electronegativity to C atoms, and thus are widely used to dope graphene. Compared with C, N has an additional electron and can hence be used as an electron donor, allowing graphene to be transformed into an n-type semiconductor and have an increased carrier density6. Therefore, N-doped graphenes (NGs) are expected to be preferable to graphene for many applications in electronic devices.

Unfortunately, doping graphene with N atoms in a controllable manner is difficult. Firstly, the doping concentration of N in graphene is usually very low (Supplementary Table 1). Although various methods such as chemical vapor deposition7, thermal annealing8, pyrolysis9, arc discharge10, hydrothermal synthesis11, and plasma treatment12 have been widely applied for the N doping of graphene, none of these methods can yield NG with an N concentration higher than 0.2, except for the carbon-nitride materials synthesized from specific aromatic hydrocarbon molecules. Such a low doping concentration greatly limits the control of the carrier concentration in NG. The second challenge for NG fabrication is preventing the doping of N as a mixture of graphitic, pyridinic, and pyrrolic N configurations, resulting in stronger scattering of the carriers during their transport and thus greatly reducing carrier mobility. To prepare NG with a desirable N configuration and concentration, it is essential to understand the conditions that promote the low doping concentration of N, as well as mixed doping types. Several studies have been undertaken to understand the formation of NG. Xiang et al. found that the effective nearest-neighbor interaction between N dopants is highly repulsive because of the strong electrostatic repulsion between N atoms, and this explains why complete nitrogen–carbon phase separation does not occur in NG13. Shi et al. have studied the N-doping limitations of NG and found that the largest achievable graphitic N-doping concentration in NG is 0.333–0.37514. Feng et al. further investigated the most stable structures of two-dimensional (2D) NG with higher N concentration and found that the 2D NGs can be stabilized at high N concentration although structures with lower N concentration are energetically more favorable15. Until now, the inherent reason for the observed doping concentration and type of N in NG has been unclear, despite previous reports.

In this work, we systematically studied the stable structures of carbon nitride with different C/N ratios (C1−xNx, 0 < x < 1), based on the particle-swarm optimization (PSO) algorithm16 and density functional theory (DFT) calculations. It is found that C1−xNx structures with lower N concentrations (or small x values) exhibit lower formation energies and thus are more thermodynamically stable with reference to the chemical potentials of C in graphene and N in N2. In addition, we found that graphitic N and pyridinic N in 2D C1−xNx structures have comparable formation energies at low N-doping concentrations. This explains current experimental observations that 2D carbon nitride films consistently exhibit lower doping concentrations in which graphitic and pyridinic N-doping coexist. With the increase of doping concentration, 2D C1−xNx structures become dominated by pyridinic N due to a significant increase in repulsive interactions between graphitic N atoms, which leads to transformation from a composition with mixed graphitic and pyridinic N to porous structures with only pyridinic N. Further, we found that the concentration of N in C1−xNx can be regulated by adjusting the feedstock used in the fabrication because this changes the chemical potential of the C or N. By appropriately modulating the chemical potential of the C and N atoms or decreasing the growth temperature, C1−xNx with high N concentration can be obtained. 2D C1−xNx structures with different N concentrations have unique electronic properties and are expected to have applications in many fields.

## Results and discussion

### Effect of N type and concentration on C1−xNx 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 sp2 hybridized and pyrrolic N is sp3 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, Ef(C1−xNx; Gr, N2) 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)} - x\mu _{{\rm{N}}({\rm{N}}_2)},$$
(1)

where $$E_{{\rm{C}}_{1 - x}{\rm{N}}_x}$$ is the free energy of C1−xNx, $$\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 N2 (for details see Supplementary Note 1). In general, this is dependent on the temperature and partial pressure of N2, 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 atoms13. 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 (C1−xNx) 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 (Ep) between N atoms is calculated by Eq. (1), where each C1−xNx structure contains only two N atoms. The calculated values of Ep 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 Ep 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 minima13.

Since there is no N–C phase separation in NG, it seems likely that we can construct C1−xNx superstructures with uniform distributions of N dopants. Figure 2b shows the calculated formation energies of the most stable structures of C1−xNx 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 C1−xNx materials increase with N concentration. The most stable structure of each C1−xNx (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 C1−xNx (x < 0.25). For C1−xNx 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.

C1−xNx 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 C1−xNx 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 C1−xNx 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 C1−xNx 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 C1−xNx structures with graphitic or pyridinic N. The N concentration of the C1−xNx structures was varied from 0 to 0.5. The local PSO algorithm was used to search all the stable structures of 2D C1−xNx (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 C1−xNx structures with high N concentration predominantly contain pyridinic N. Interestingly, further calculations demonstrate that the coexistence of pyridinic and graphitic N in 2D C1−xNx decreases the formation energies of these structures (Supplementary Figs. 6 and 7). In particular, we found that g-C3N4 (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 C1−xNx 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 concentrations7,18,19 (Supplementary Table 1), and particularly with the stability of experimentally synthesized g-C3N4, which contains both graphitic and pyridinic N18.

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 concentrations19.

Of all the structures considered, C3N (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 C3N 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 structure13. It should be noted that the graphitic N-doped C3N (x = 0.25) has already been synthesized and characterized in recent experiments20,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 C1−xNx 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 C2N (Supplementary Fig. 9) and g-C3N4 (Supplementary Fig. 10). Similarly to the 2D C1−xNx structures discussed above, the formation energies of both C2N and g-C3N4 increase with addition of pyrrolic N defects. Considering systems with the same proportion of pyrrolic N defects, but in different positions, C2N (or g-C3N4) with large distances between the defects leads to a lower formation energy of the structure. (Supplementary Figs. 9 and 10). Furthermore, for g-C3N4 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 C1−xNx

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 N2 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 parameters22,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 C1−xNx 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 C1−xNx 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 N2 and μC is increased (Fig. 5b), the formation energies of C1−xNx structures decrease, but structures with low N concentrations have a more substantial reduction in energy. Therefore, C1−xNx 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 C1−xNx materials with a higher percentage of graphitic N. To increase the proportion of C1−xNx 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 CmHn (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}RT\ln \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, PX is the partial pressure of X, P0 = 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}RT\ln \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 C1−xNx at temperature of T, and pressure of P, from CmHn and NkHl, Ef(C1−xNx; CmHn, NkHl, 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 C1−xNx (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, P0 = 0.1 MPa, $$P_{{\rm{H}}_2} = P_{\rm{feedstock}} = 10^{ - 6}\,{\mathrm{MPa}}$$), the formation energies of C1−xNx can be calculated. Figure 5c shows the effect of different N feedstocks, when CH4 is used as the C feedstock. The N feedstocks considered are N2, NH3, and N2H4. It is clear that N2 favors the formation of C1−xNx with low N concentration, whereas N2H4 enhances the formation probability of C1−xNx with high N concentration. Figure 5d shows the effect of different C feedstocks, when NH3 is used as the N feedstock. The C feedstocks considered are CH4, C2H4, and C2H2. We can see that use of C2H2 makes the free energy curve flatter than with CH4 or C2H4. This means the formation of C1−xNx 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 CH4 and NH3 as the feedstock, the synthesized NGs have both pyridinic and graphitic N24, while Luo et al. observed the synthesized NGs were composed purely of pyridinic N by changing the feedstock to C2H4 and NH325.

Equations (2) and (3) demonstrate that the growth temperature can also be used to tune the structures of C1−xNx by changing the chemical potential of C and N. This is demonstrated in Fig. 5e where the effect of temperature on a system with CH4 and NH3 feedstock and typical experimental pressures is shown. Higher temperatures lead to the production of C1−xNx 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 temperature26,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 N28,29. The pressure also has significant effect on the formation energies of the C1−xNx structures. Compared with the case where the partial pressures of H2, C and N feedstocks are fixed as 10−5 bar (black line + circle in Fig. 5f), decreasing the pressure ratio of C feedstock and H2 ($$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 C1−xNx structures with high N-doping concentration and more pyridinic N. However, increasing the pressure ratio of C feedstock and H2 ($$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 C1−xNx structures with low N-doping concentration and more graphitic N. In contrast, decreasing the pressure ratio of N feedstock and H2 ($$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 C1−xNx structures with low N-doping concentration and more graphitic N, while increasing the pressure ratio of N feedstock and H2 ($$P_{\rm{N}}/P_{{\rm{H}}_2} = 10$$, red line + star in Fig. 5f) favors the formation of C1−xNx structure with high N-doping concentration and more pyridinic N.

### Electronic properties of C1−xNx

The structure of 2D C1−xNx 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 C1−xNx structures with graphitic N, most structures are metallic except C12N and C3N, which are semiconductors with medium band gaps (Supplementary Fig. 11). The 2D C1−xNx structures with pyridinic N, C12N, C8N, C6N, and C10N3 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 C1−xNx structures, the HSE06 functional was used to calculate the bandgaps of C12N, C3N, C2N, and CN, of which C3N and C2N 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 C3N (x = 0.25) composed of graphitic N has a bandgap of 1.22 eV. Experimentally, the measured bandgaps of C3N are highly dependent on the flake size of the materials. When the size of C3N quantum dots (QDs) varies from 1.8 to 5.5 nm, the bandgap changes from 2.74 to 1.57 eV20,21. The calculated bandgap of C2N (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 C2N QDs changes from 3.8 to 4.8 nm30,31. The HSE06 calculations demonstrate that C12N (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., C3N) are much higher than the structures composed of pyridinic N (e.g., C2N), (see Supplementary Note 2, Table 2, and Supplementary Fig. 14). These studies demonstrate that controlling the structure of C1−xNx 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 C1−xNx 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.

## Methods

### Structure searching

The structures of C1−xNx for various values of x were predicted using the local PSO algorithm, as implemented in the crystal structure analysis by particle swarm optimization software16. Structure prediction was for a fixed cell of C1−xNx with no more than 30 atoms and the population size is set to 30. For each step of the searching simulation, 80% of the predicted structures passed to the next step the other 20% of the structures were randomly generated. The structural searching simulations for each system were stopped after ~35 generations (1050 generated structures). Based on the PSO search results, ten to fifteen different structures of each C1−xNx were selected for further structural relaxation using DFT calculations as implemented in Vienna ab initio simulation package32,33.

### First-principles calculations

For the DFT calculations, the projector-augmented-wave method was used to mimic the ionic cores34,35. The generalized gradient approximation (GGA) with the PBE exchange-correlation function was used with the plane-wave cutoff energy set at 400 eV for all calculations36. The bandgaps of the C1−xNx structures were obtained using the nonlocal hybrid functional (HSE)37, where the percentage (known as the mixing parameter α) of the semilocal GGA exchange potential was replaced by screened Fock exchange36. The space between the C1−xNx layers was set to be at least 15 Å to eliminate the interaction between the layers. The geometries of the structures were relaxed until the force on each atom was less than 0.01 eV Å−1, and the energy convergence criterion of 1 × 10−5 eV was met. The Brillouin zone was represented by a Monkhorst–Pack special k-point mesh (details are given in Supplementary Note 3) for the geometry optimization of C1−xNx structures38.