Louis IV (; 1 April 1282 – 11 October 1347), called the Bavarian (, ), was King of the Romans from 1314, King of Italy from 1327, and Holy Roman Emperor from 1328 until his death in 1347.

Louis' election as king of Germany in 1314 was controversial, as his Habsburg cousin Frederick the Fair was simultaneously elected king by a separate set of electors. Louis defeated Frederick in the Battle of Mühldorf in 1322, and the two eventually reconciled. Louis was opposed and excommunicated by the French Pope John XXII; Louis in turn attempted to depose the pope and install an anti-pope.

Louis IV was Duke of Upper Bavaria from 1294 to 1301 together with his elder brother Rudolf I, was Margrave of Brandenburg until 1323, and Count Palatine of the Rhine until 1329, and became Duke of Lower Bavaria in 1340. He was the last Bavarian to be a king of Germany until 1742. He became Count of Hainaut, Holland, Zeeland, and Friesland in 1345 when his wife Margaret inherited those domains.

Origins and Youth Louis was born in Munich, the son of Louis II, Duke of Upper Bavaria and Count Palatine of the Rhine, and Matilda, a daughter of King Rudolph I. As a member of the noble house of Wittelsbach, his great-great-grandfather, Otto, was given the duchy of Bavaria for his services to Frederick Barbarossa. As a result, Louis and his family enjoyed a close relationship with the Hohenstaufen dynasty, who not only acted as political allies to the Wittelsbachs, but also as family members through Elizabeth of Bavaria's marriage to Conrad IV of Germany.

Louis's father, Louis the Strict, went to great pains to ensure the Wittelsbach family would be closely intermarried with the other magnates of the empire. For example, at the coronation of Rudolf of Habsburg, Louis the Strict managed to negotiate a marriage to his daughter, Matilda. Due to his ties with the Habsburg dynasty, Louis IV was raised at the court of Albert of Habsburg. As a result, he was educated alongside his playmate Frederick the Fair, who would later become his rival in the struggle for the German throne. Ludwig's father died in 1294, and on the 14th of October 1308, he married the Piast princess Beatrice of Silesia.

In 1310, there was a dispute between the brothers over the inheritance of their father, Louis the Strict. He divided his realm so that Louis IV would rule with his brother Rudolf I in the Electoral Palatinate and the Duchy of Upper Bavaria. In Lower Bavaria, after the death of Stephen I on December 1310, tensions broke out with Louis and the Habsburgs over the succession. Even though Otto III, the former King of Hungary, had chosen Louis, local nobles instead appointed Frederick the Fair as the legal guardian of Stephen's children, Otto IV and Henry XIV. This led to conflict between Louis and Frederick, although he soon made a major change of course: In the Peace of Munich on the 21st of June 1313, he made peace with his brother and agreed a method of joint rulership for Upper Bavaria.

The peace was only intended to last one year, but Louis used this period to maneuver carefully with the aims of defeating Frederick. Louis promptly resumed hostilities, eventually defeating Frederick in the Battle of Gammelsdorf in November of that same year, despite Leopold I sending aid to Frederick. This victory caused a stir within the Holy Roman Empire and increased the reputation of the Bavarian Duke, and forced Frederick to surrender the tutelage of the young dukes.

Election as German King and conflict with Frederick the Fair

The death of Holy Roman Emperor Henry VII in August 1313 led to a 14-month long election period between the seven prince-Electors. Henry's son John, King of Bohemia since 1310, was initially supported by the Bohemians alongside their allies Archbishop Peter of Aspelt of Mainz and Archbishop Baldwin of Luxembourg of Trier. Philip IV of France, who had already had relatives on the thrones of Hungary and Naples, wanted to ensure the election of his son Philip as Holy Roman Emperor after failing the previous election. With Philip being entirely unsupported by the Imperial princes yet again, John's position was initially strong.

However, believing John of Bohemia to be too young and the Luxembourg dynasty too powerful, the Habsburgs pushed forth with the only realistic resistance to the pro-Luxembourg faction. As the son of Emperor Henry's predecessor, Albert I, Frederick the Fair began gathering support for his cause. This intimidated the pro-Luxembourg faction, causing the Archbishops of Mainz and Trier to convince John to step down and settle on Louis as a compromise candidate for Emperor in order to prevent their Habsburg rivals from attaining the Imperial throne.

On 19 October 1314, Archbishop Henry II of Cologne chaired an assembly of four electors at Sachsenhausen, south of Frankfurt. Participants were Louis' brother, Rudolph I of the Palatinate, who objected to the election of his younger brother, Duke Rudolph I of Saxe-Wittenberg, and Henry of Carinthia, whom the Luxembourgs had deposed as King of Bohemia. These four electors chose Frederick as King.

The Luxembourg party did not accept this election and the next day a second election was held. Upon the instigation of Peter of Aspelt, Archbishop of Mainz, five different electors convened at Frankfurt and elected Louis as King. These electors were Archbishop Peter himself, Archbishop Baldwin of Trier and King John of Bohemia – both of the House of Luxembourg – Margrave Waldemar of Brandenburg and Duke John II of Saxe-Lauenburg, who contested Rudolph of Wittenberg's claim to the electoral vote.

This double election was quickly followed by two coronations on the 25th of November: Louis was crowned at Aachenthe customary site of coronationsby Archbishop Peter of Mainz, while the Archbishop of Cologne, who by custom had the right to crown the new king, crowned Frederick at Bonn. Ludwig did not have the authentic Imperial Regalia, and so needed to have replicas created in an attempt to legitimise his coronation. Chroniclers on both sides attempted to portray their rival's coronation as illegitimate. For example, in the anti-Habsburg Chronica Ludovici, Frederick is depicted as being raised on a barrel on his coronation, before falling inside it. In the following conflict between the kings, Louis recognized in 1316 the independence of Switzerland from the Habsburg dynasty.

The fraught political situation was ripe for papal intervention. Pope Clement V died on the 20th of April 1314, half a year before the kings were elected. The throne of St. Peter would remain empty for two more years. Without a religious authority to adjudicate, both sides instead came to believe that God favoured whichever side won on the battlefield. However, despite this belief, the kings were reluctant to actually take to combat between 1314 and 1322. Frederick the Fair's defeat at the Battle of Morgarten gave him further cause for restraint in the struggle for the throne. Regardless of this apprehension, minor skirmishes took place between the two sides in this period: Speyer and Buchloe in 1315, Esslingen in 1316, Mühldorf in 1319, and Strasbourg in 1320. No major battle had yet taken place, however. The next few years, however, were unfortunate for Louis. Neither side was able to take advantage of the death of Waldemar, Margrave of Brandenburg-Stendal, and after the death of the Archbishop of Mainz, Pope John XXII appointed Matthias von Bucheck, a follower of the Habsburgs, as his successor. Previously, the Papacy had not taken sides in the conflict - but in 1316, Pope John officially expressed his opposition to Louis IV. Supported by both the Pope his brother Leopold, Frederick's victory seemed within grasp.

A few weeks before the deciding battle of the war, Louis's first wife Beatrix died on August 1322. Three of his six children from this marriage had reached adulthood: Matilda, Louis, and Stephen. On the 28th of September 1322, Frederick's army was decisively defeated in the Battle of Mühldorf at Ampfing Heath, where Frederick and 1300 nobles from Austria and Salzburg were captured. Louis's ally Frederick contributed greatly to the victory, and it is also believed that the Fürstenfeld Abbey played a vital role in secretly sending messages to Louis's army. As a result of the abbey's aid, it was granted special privileges by Louis out of gratitude.

Louis held Frederick captive in Trausnitz Castle (Schwandorf) for three years, but his reign was far from secured. After using the title King of the Romans without Papal approval, as well as granting offices and estates in Northern Italy without consulting the Papal States, which angered the Pope. He warned Louis that he had three months to addicate and revoke all his decrees, and upon failing to do son, Pope John XXII decided to excommunicate Louis. Louis reacted by making three appeals: The Nuremberg Appeal in 1323, the Frankfurt Appeal in January 1324, and the Sachsenhausen Appeal in May 1324. He insisted that the Electors had chosen him as Emperor, making his rule righteous. However, these pleas were not paid any heed by the Pope. Instead, he ordered Louis to give up his kingship, and threatened to excommunicate his followers, take away his imperial fiefs, and even his original Duchy of Bavaria if he did not comply. Frederick's brother, Leopold, sought to take advantage of the situation, and began to organise his banner as the honorable resistance to Frederick's rule.

In light of the recent retreat of John of Bohemia from the alliance, as well as the Papal excommunication and resistance from the Habsburgs, Louis attempted to come to a compromise. On the 13th of March 1325, Louis freed the imprisoned Frederick, and in a secret agreement, made Frederick renounce the crown and any Habsburg imperial fiefs. Furthermore, he had to accept that Louis was the legal custodian of the young Wittelsbach brothers. After Frederick accepted these terms, he was freed from captivity. Frederick did not have to pay a ransom, although he was forced to part with all of his Imperial possessions. The Peace of Trausnitz between Louis and Frederick was made with both signature and verbal agreement, and was made formally at Easter. The two heard Mass and received communion together. The value of oaths was very high in the Medieval period, and thus Frederick made a great concerted effort to genuinely reconcile with Louis. Part of this reconciliation involved a promise that he would voluntarily return to prison if he did not convince his brothers to submit to Louis. He also agreed to oppose the Pope for excommunicating Louis, and arranged a marriage between Louis's son Stephen and Frederick's daughter Elizabeth.

Dual Kingship

As he did not manage to overcome Leopold's obstinacy, Frederick returned to Munich as a prisoner, even though the Pope had released him from his oath. Louis, who was impressed by such nobility, renewed the old friendship with Frederick, and they agreed to rule the Empire jointly. Since the Pope and the electors strongly objected to this agreement, another treaty was signed at Ulm on 7 January 1326, according to which Frederick would administer Germany as King of the Romans, while Louis would be crowned as Holy Roman Emperor in Italy. Such a system of dividing kingship had never been seen before in the Holy Roman Empire, and it would never truly be repeated again in Imperial history. Louis also took further action in order to placate Leopold, and sought the advice of Frederick the Fair in secret meetings in order to achieve these ends. As a result, Leopold was appointed as an imperial vicar, with the aim of ensuring his loyalty to the two kings. This was short lived, however - after Leopold's death in 1326, Frederick withdrew from the regency of the Empire and returned to rule only Austria. He died on 13 January 1330.