Both onions and garlic contain sulfur-based amino acids, which break down into various compounds.
Slicing an onion triggers the release of an enzyme that decomposes one of these compounds, forming a volatile substance known as propanethial-S-oxide, which can cause tears.
When this substance reacts with water, such as in your eyes, it undergoes hydrolysis, producing propanal, sulfuric acid, and hydrogen sulfide.
The eyes react tearfully to dilute the resulting acid, causing tearing.
Despite this tearful reaction, these same sulfur compounds are responsible for the pleasant aroma emitted during the cooking of onions.