Louis IV (; 1 April 1282 – 11 October 1347), called the Bavarian, of the house of Wittelsbach, was King of the Romans from 1314, King of Italy from 1327, and Holy Roman Emperor from 1328. 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. Early reign as Duke of Upper Bavaria 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. Though Louis was partly educated in Vienna and became co-regent of his brother Rudolf I in Upper Bavaria in 1301 with the support of his Habsburg mother and her brother, King Albert I, he quarreled with the Habsburgs from 1307 over possessions in Lower Bavaria. A civil war against his brother Rudolf due to new disputes on the partition of their lands was ended in 1313, when peace was made at Munich. In the same year, on November 9, Louis defeated his Habsburg cousin Frederick the Fair who was further aided by duke Leopold I. Originally, he was a friend of Frederick, with whom he had been raised. However, armed conflict arose when the guardianship over the young Dukes of Lower Bavaria (Henry XIV, Otto IV, and Henry XV) was entrusted to Frederick, even though the late Duke Otto III, the former King of Hungary, had chosen Louis. On 9 November 1313, Frederick was defeated by Louis in the Battle of Gammelsdorf and had to renounce the tutelage. This victory caused a stir within the Holy Roman Empire and increased the reputation of the Bavarian Duke. Election as German King and conflict with