If while studying Torah or praying a person smells a foul odor resulting from flatulence, is he required to stop studying or praying until the odor dissipates?
With regard to Torah study, the Shulchan Aruch (79:9; listen to audio for precise citation) distinguishes between a case where one himself caused the odor, and where it was caused by somebody else. The person who caused the odor must discontinue his learning until the smell dissipates, whereas others may continue learning even though they smell the odor. The Shulchan Aruch explains that students would often sleep in the study hall, during which time they cannot restrain themselves from passing air. Had the Sages forbade studying when smelling an odor caused by somebody else, students would have to interrupt their learning very frequently as a result of the students sleeping in the room. The Rabbis were therefore lenient in this regard and allowed a student to continue learning even if there is an odor, provided that it was caused by somebody else.
These guidelines apply only to Torah study. When it comes to the recitation of Shema and the Amida, however, one must discontinue his recitation upon smelling a foul odor, regardless of whether it originates from him or from somebody else, and wait for it to dissipate before resuming his prayer. Nevertheless, if somebody did continue reciting Shema or the Amida while smelling a foul odor, his recitation is valid and he need not repeat the Shema or Amida, though in the case of Shema one should preferably repeat the recitation, without the Berachot. (See Halacha Berura, Helek 5, page 110.)
Summary: If a person smells an odor caused by flatulence as he studies Torah, he may continue studying despite the unpleasant smell, unless it originated from him, in which case he must discontinue his learning until the odor dissipates. If one smells such an odor while reciting Shema or the Amida, he must in all cases stop his recitation and wait for the odor to pass, though if he did continue praying while smelling the odor, his recitation is nevertheless valid. In the case of Shema, however, one should preferably repeat the Shema recitation after the smell dissipates.