Because the slab extends infinitely in the xy plane, the electric field lies only along the z direction. The differential form of Gauss' Law for such a one-dimensional electric field is $$\frac{dE}{dz}=\frac{\rho(z)}{\epsilon_0}$$ This can be integrated, using the boundary condition that the electric field must be zero at large distances from the slab, because it is electrically neutral.

A more elementary way of solving the problem is to divide the slab into infinitesimally thin layers of thickness $dz$. The electric field of each layer is uniform, independent of distance from the layer, and is $dE=\frac{d\sigma}{2\epsilon}$ pointing away from the layer on each side for +ve surface charge density $d\sigma=\rho(z)dz$. The total electric field at any point inside or outside of the slab is found by superposition of fields from every such layer in the slab.

**Note** that the total electric field at any point due to all layers which are closer to the centre plane $z=0$, is zero. This is because the charge density is anti-symmetric : for every layer of +ve area charge density on one side of $z=0$ there is a layer of -ve charge with the same magnitude of area density on the other side of $z=0$. The electric fields of these two layers cancel out for points which outside of the two layers, in the same way that the total electric field is zero outside of a parallel plate capacitor (if the plate dimensions are very much bigger than the distance from them).

From this observation you can see that the electric field outside of the slab is zero, because all layers in the slab are closer to the centre plane.

The simplest way of getting the field **inside** the slab is to apply Gauss' Law using a "pill box" Gaussian surface which has one face A of area S at the surface of the slab $z=a$ (where $E(a)=0$) and the other face B at distance $|z|<a$ from the centre plane. The other face(s) of the pill box are parallel to the z direction so the electric flux through them is zero. There is no electric flux through face A; the flux through face B is $E(z)S=\frac{q}{\epsilon_0}$ where by integration $$q=\int_a^z S\rho(z)dz$$ is the total charge inside the pill box.

See Electric field in a non-uniformly charged sheet. An answer identical to mine is given in the duplicate question Finding the electric field of a NON uniform slab?

reshown May 17, 2019 by sammy gerbil