Childhood and youth (1326–1342)
Born on 5 March 1326, Louis was the third son of Charles I of Hungary and his wife, Elizabeth of Poland. He was named for his father's uncle, Louis, Bishop of Toulouse, canonized in 1317. The first-born son of his parents, Charles, died before Louis was born. Louis became his father's heir after the death of his brother Ladislaus in 1329.
He had a liberal education by the standards of his age and learned French, German and Latin. He showed a special interest in history and astrology. A cleric from Wrocław, Nicholas, taught him the basic principles of Christian faith. However, Louis's religious zeal was due to his mother's influence. In a royal charter, Louis remembered that in his childhood, a knight of the royal court, Peter Poháros, often carried him on his shoulders. His two tutors, Nicholas Drugeth and Nicholas Knesich, saved the lives of both Louis and his younger brother, Andrew, when Felician Záh attempted to assassinate the royal family in Visegrád on 17 April 1330.
Louis was only nine when he stamped a treaty of alliance between his father and John of Bohemia. A year later, Louis accompanied his father in invading Austria. On 1 March 1338, John of Bohemia's son and heir, Charles, Margrave of Moravia, signed a new treaty with Charles I of Hungary and Louis in Visegrád. According to the treaty, Charles of Moravia acknowledged the right of Charles I's sons to succeed their maternal uncle, Casimir III of Poland, if Casimir died without a male issue. Louis also pledged that he would marry the margrave's three-year-old daughter, Margaret.
Casimir III's first wife, Aldona of Lithuania, died on 26 May 1339. Two leading Polish noblemen – Zbigniew, chancellor of Cracow, and
Spycimir Leliwita – persuaded Casimir, who had not fathered a son, to make his sister, Elizabeth, and her offspring his heirs. According to the 15th-century Jan Długosz, Casimir held a general sejm in Cracow where "the assembled prelates and nobles" proclaimed Louis as Casimir's heir, but the reference to the sejm is anachronistic. Historian Paul W. Knoll writes that Casimir preferred his sister's family to his own daughters or a member of a cadet branch of the Piast dynasty, because he wanted to ensure the king of Hungary's support against the Teutonic Knights. Louis's father and uncle signed a treaty in Visegrád in July whereby Casimir III made Louis his heir if he died without a son. In exchange, Charles I pledged that Louis would reoccupy Pomerania and other Polish lands lost to the Teutonic Order without Polish funds and would only employ Poles in the royal administration in Poland.
Louis received the title of Duke of Transylvania from his father in 1339, but he did not administer the province. According to a royal charter from the same year, Louis's bride, Margaret of Bohemia, lived in the Hungarian royal court. Louis's separate ducal court was first mentioned in a royal charter of 1340.