The Torah commands in Parashat Shoftim (Debarim 23:15), "Ve'haya Mahanecha Kadosh" – "Your camp shall be sacred." The Gemara (Shabbat 150a) interprets the word "camp" in this verse as a reference to any place used for prayer or Torah study, such that the Torah here instructs us to ensure that any such place must be clean, without filth, to preserve the sanctity of our prayers and Torah learning.
Accordingly, it is strictly forbidden to even silently think Torah matters in an unclean place, such as the restroom. It goes without saying that one may not speak Torah matters in the restroom.
The severity of this prohibition is expressed by the Gemara elsewhere, in Masechet Berachot (24b), in regard to the prohibition against reciting Shema in a place with exposed sewage. The Gemara applies to a person who does not discontinue his reading when encountering filth a number of harsh warnings in the Prophets, condemning those who desecrate the Misvot. Conversely, the Gemara teaches, those who observe this command, and refrain from engaging in sacred matters when in the presence of filth, are rewarded with longevity.
In order to avoid thinking of Torah matters in the restroom, it is proper for one to actively think about other matters, such as his finances, his business or his work responsibilities. On Shabbat, when one must avoid thoughts about such matters, as they induce stress, one should try while using the restroom to think of pleasant matters such as aesthetically pleasing things that he has recently seen, and the like.
One must not think about or study in the restroom things that may evoke thoughts of Torah. Thus, for example, one must not study Hebrew grammar in the restroom, as this will invariably lead to thoughts about the Biblical origins of different words. It is likewise forbidden to read Hebrew poetry in the bathroom, as much of Hebrew poetry draws upon passages from the Tanach and rabbinic literature.
One may read secular newspapers in the restroom. Religious newspapers may be read only if they do not contain words of Torah. For this reason, some religious newspapers in Israel present Torah materials in a separate insert, so the paper itself may be brought into the restroom.
The Poskim debate the question of whether one may contemplate the six constant Misvot while in the restroom, such as the belief in G-d, the oneness of G-d, and the fear of G-d. The Lebusheh Mordechai (Rav Mordechai Yehuda Leib Winkler, Hungary, 1844-1932) ruled that this is forbidden. The Sitz Eliezer (Rav Eliezer Waldenberg, Jerusalem 1915-2006) agreed with this ruling, asserting that although one must certainly have these beliefs in one's mind at all times, he should not consciously think about them while in the restroom. By contrast, the Hochmat Shlomo (Rav Shlomo Kluger, 1785-1869) allowed contemplating G-d's existence while in the restroom, and Rav Haim Kanievsky concurred with this ruling, adding that one must avoid thinking about any verses or Torah texts in the process.
Ideally, one should spend his time in the restroom contemplating his own lowliness. These moments are a perfect opportunity for a person to work on his humility, as he recognizes his limitations. Indeed, the Maggid – the angel that taught Torah to Maran Rav Yosef Karo, author of the Shulhan Aruch – instructed him to contemplate his lowliness while using the restroom.
If one is the middle of studying a complex, intricate Torah topic when he experiences a need to use the restroom, he should go to the restroom without delay, and make an effort to avoid thinking about the material he is studying.
If one enters a public restroom and sees his fellow doing something forbidden, he may tell him that he must discontinue the behavior. One example might be seeing somebody trimming his hair in the restroom during the afternoon of Ereb Pesach, when haircutting is forbidden. Telling the person to stop cutting his hair does not violate the prohibition against speaking Torah matters in the restroom. The Mishna Berura writes that in such a case, one should not state explicitly that the action is forbidden, as he would then be articulating a Halacha in the restroom. He should instead say, "This is not done," or "You must stop," etc. Other Poskim, however, allow explicitly stating that the act is forbidden, and the Mishna Berura himself brings this lenient position. One may rely on this lenient view, especially if otherwise the fellow might not understand that he is acting in a forbidden manner.
When designing a synagogue, care should be taken to ensure that the restroom is not near the sanctuary or study hall, for two reasons – so that the odor from the restroom does not reach the sanctuary or study hall, and so that people using the restroom do not hear the prayers or words of Torah. If, for whatever reason, the restroom is situated near the sanctuary or study hall, a person who needs the restroom may nevertheless use it, and should make an effort to divert his attention away from the prayers or words of Torah that are being spoken nearby.
This discussion is important not only for the actual Halachot that must be observed, but also because of the underlying assumption that one's mind is naturally inclined to think about Torah matters. The Poskim addressed this topic at length precisely because they deemed it natural for a person's mind to be occupied with Torah thoughts, thus necessitating a concentrated effort to avoid such thoughts in the restroom or in the presence of filth. The requirement to avoid Torah thoughts in the restroom should remind us of the requirement to keep our minds occupied with Torah the rest of the time, to make Torah study a priority in our lives and devote our free time to Torah learning, so that it is what fills our mind.

